mypy

How does `mypy` know the signature of the pydantic model?

How does `mypy` know the signature of the pydantic model? Question: How does mypy know the signature of the pydantic model in this manner? from pydantic import BaseModel class Model(BaseModel): a: int Model(a=’asd’) # error: Argument "a" to "Model" has incompatible type "str"; expected "int" How can pydantic BaseModel‘s metaclass change the __init__ signature? Asked …

Total answers: 1

How to make mypy like my protocols that work at runtime with runtime_checkable

How to make mypy like my protocols that work at runtime with runtime_checkable Question: I defined a couple of protocols like so: import json from typing import Any, Protocol, TypeVar, runtime_checkable T_co = TypeVar(‘T_co’, covariant=True) T = TypeVar(‘T’) @runtime_checkable class SupportsRead(Protocol[T_co]): def read(self, __length: int = …) -> T_co: … @runtime_checkable class SupportsWrite(Protocol[T_co]): def write(self, …

Total answers: 1

bounds with typyvar

bounds with typyvar Question: I want to make sure that the input is the same as output, I tried to use TypeVar to bound the typing but I can’t get it to work… from typing import TypeVar, Union, Any import pandas as pd class my_Class(): def __init__(self): self.days = [] self.count = 0 def __call__(self, …

Total answers: 2

reveal_type gives me a type, but it does not exist

reveal_type gives me a type, but it does not exist Question: So, I have a code that typechecks, but when I try to run it, it complains about the type not existing import pyrsistent a = pyrsistent.pset([1,2,3]) #reveal_type(a) #mypy file.py, gives type pyrsistent.typing.PSet[buildins.int] #b : pyrsistent.typing.PSet[int] = a #when running: AttributeError: module ‘pyrsistent’ has no …

Total answers: 1

Type hinting decorator with bounded arguments

Type hinting decorator with bounded arguments Question: An example of what I’d like to do is as follows: @dataclass class Arg1: x = field() @dataclass class Arg2: x = field() @dataclass class Obj: y = field() T = TypeVar("T") R = TypeVar("R", bound=(Arg1 | Arg2)) C = TypeVar("C", bound=Callable[[Self, R], T]) @staticmethod def deco(f: C) …

Total answers: 2

Why does Mypy report a subset of JSON as invalid?

Why does Mypy report a subset of JSON as invalid? Question: I have a base class that returns a JSON type. I then have subclasses that return more specific types that should I think be valid JSON types, but Mypy reports an error: error: Return type "List[Dict[str, JSON]]" of "return_json" incompatible with return type "JSON" …

Total answers: 1

python abstract instance property alternative

python abstract instance property alternative Question: If I have for example: class Parent(object): @property @abc.abstractmethod def asdf(self) -> str: """ Must be implemented by child """ @dataclass class Children(Parent): asdf = "1234" def some_method(self): self.asdf = "5678" I get an error from mypy saying I am shadowing class attribute in some_method. This is understandable, since …

Total answers: 1

Can't get rid of mypy error about wrong type numpy.bool_ and bool

Can't get rid of mypy error about wrong type numpy.bool_ and bool Question: I have a classe containing several np.array : class VECMParams(ModelParams): def __init__( self, ecm_gamma: np.ndarray, ecm_mu: Optional[np.ndarray], ecm_lambda: np.ndarray, ecm_beta: np.ndarray, intercept_coint: bool, ): self.ecm_gamma = ecm_gamma self.ecm_mu = ecm_mu self.ecm_lambda = ecm_lambda self.ecm_beta = ecm_beta self.intercept_coint = intercept_coint I want to …

Total answers: 1

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