Skip to content

Re-implementing NewType in python #7901

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lovasoa opened this issue Nov 7, 2019 · 1 comment
Closed

Re-implementing NewType in python #7901

lovasoa opened this issue Nov 7, 2019 · 1 comment

Comments

@lovasoa
Copy link

lovasoa commented Nov 7, 2019

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,
from typing import Type, TypeVar

T = TypeVar("T")
def MyNewType(name: str, tp: Type[T]) -> Type[T]:
    def new_type(x):
        return x

    new_type.__name__ = name
    new_type.__supertype__ = tp
    return new_type

Email = MyNewType("Email", str)

class MySchema:
    email: Email
  • What is the actual behavior/output?
/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
@ilevkivskyi
Copy link
Member

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants