mypy

mypy indexing pd.DataFrame with an Enum raises no overload variant error

mypy indexing pd.DataFrame with an Enum raises no overload variant error Question: The issue Mypy gives no overload variant of __getitem__ of "DataFrame" matches argument type "MyEnum" error. In this case the argument type is an Enum but the issue would occur for any other custom type. Here is the signature of __get_item__ below. def …

Total answers: 1

Mypy type compatible with list, tuple, range, and numpy.array?

Mypy type compatible with list, tuple, range, and numpy.array? Question: The code import numpy as np def join(v:list, delim:str = ","): """ join the elements of v using the given delimiter """ return delim.join(str(x) for x in v) print(join([0,1,2,3])) print(join((0,1,2,3))) print(join(range(4))) print(join(np.array(range(4)))) runs, but mypy only likes the first call to join and says x.py:8: …

Total answers: 1

mypy doesn't understand class and interface are the same

mypy doesn't understand class and interface are the same Question: from abc import ABC, abstractmethod class IState(ABC): """Interface para o padrĂ£o de projeto State.""" @abstractmethod def sucesso_ao_pagar(self) -> None: pass @abstractmethod def despachar_pedido(self) -> None: pass @abstractmethod def cancelar_pedido(self) -> None: pass class Pedido: """Classe que representa um pedido.""" def __init__(self) -> None: self.estado_atual = …

Total answers: 1

Pylance reports incorrect error for openpyxl

Pylance reports incorrect error for openpyxl Question: I’m using VSCode for python and am getting a typing error with Pylance that appears incorrect and is not reported as an error by mypy –strict. from openpyxl import load_workbook wb = load_workbook("c:/temp/file.xlsx") ws = wb["Sheet1"] x = ws["A1"] Pylance reports "__getitem__" method not defined on type "Chartsheet" …

Total answers: 1

Are there any alternative to this overload case in python with typing?

Are there any alternative to this overload case in python with typing? Question: currently I am implementing a lot of subclasses that should implement a function, this is a minimal example of the current project. In this case, I have a function that needs to call, to the login of a user, but depending of …

Total answers: 1

How to handle Unsupported operand types in mypy when type should be clear?

How to handle Unsupported operand types in mypy when type should be clear? Question: Consider the following toy example. class MyClass: def __init__(self, optional_arg: float | None = None) -> None: self.optional_arg = optional_arg self._parse_args() def _parse_args(self) -> None: if self.optional_arg is None: self.optional_arg = 0.5 def multiply(self) -> float: # mypy is complaining with …

Total answers: 2

Is it possible to programmatically generate a pyi file from an instantiated class?

Is it possible to programmatically generate a pyi file from an instantiated class? Question: I’m creating a class from a dictionary like this: class MyClass: def __init__(self, dictionary): for k, v in dictionary.items(): setattr(self, k, v) I’m trying to figure out how I can get Intellisense for this dynamically generated class. Most IDEs can read …

Total answers: 1

mypy complains about classmethod

mypy complains about classmethod Question: I have a trivial dataclass (from pydantic) from pydantic.dataclasses import dataclass from abc import ABCMeta from abc import abstractmethod @dataclass class BaseEntity(metaclass=ABCMeta): @classmethod @abstractmethod def from_dict(cls, other: dict): … @abstractmethod def dict(self): … @dataclass class UserEntity(BaseEntity): id: Optional[str] name: str email: str avatar: str @classmethod def from_dict(cls, other: dict): return …

Total answers: 2

Typehinting function with two possible call signatures

Typehinting function with two possible call signatures Question: I have a class which takes a function as a parameter, and I want that function to either have signature int -> int or list[int] -> int. I’ve type hinted it using a Union as follows. from typing import Callable, Union class Foo: def __init__(self, func: Callable[[Union[int, …

Total answers: 1

Generic vs Specific MyPy types of functions

Generic vs Specific MyPy types of functions Question: I remember reading, or hearing somewhere that for a function, input types should be as generic as possible (Iterable over list), but return types should be as specific as possible. Is this written down somewhere official that I can reference when this comes up in team discussions? …

Total answers: 1