You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Python provides a handy NewType function that allows the creation of new types. If I'm not mistaken, this function's behavior is hardcoded in mypy and it's not possible to reproduce it in python.
There are several cases where it would be useful to be able to extend this function, and still have the code typecheck.
One example use case is the NewType function in marshmallow-dataclass (defined here)
Please insert below the code you are checking with mypy,
/tmp/test.py:9: error: "Callable[[Any], Any]" has no attribute "__supertype__"
/tmp/test.py:10: error: Incompatible return value type (got "Callable[[Any], Any]", expected "Type[T]")
/tmp/test.py:15: error: Variable "test.Email" is not valid as a type
What is the behavior/output you expect?
The code would typecheck
What are the versions of mypy and Python you are using?
mypy 0.720
Python 3.7.5
What are the mypy flags you are using? (For example --strict-optional)
none
The text was updated successfully, but these errors were encountered:
Yes, typing.NewType is a special syntactic form mypy's point of view (it never parses its source code). I don't think we will ever support type system feature that would be able to express this kind of dynamic type creation. Your best bet is to write a small mypy plugin using the get_dynamic_class_hook() (you can use e.g. plugin that ships with sqlalchemy-stubs as an example).
Python provides a handy
NewType
function that allows the creation of new types. If I'm not mistaken, this function's behavior is hardcoded in mypy and it's not possible to reproduce it in python.There are several cases where it would be useful to be able to extend this function, and still have the code typecheck.
One example use case is the
NewType
function in marshmallow-dataclass (defined here)The code would typecheck
mypy 0.720
Python 3.7.5
none
The text was updated successfully, but these errors were encountered: