-
-
Notifications
You must be signed in to change notification settings - Fork 34.7k
bpo-34963: Create callable types in typing.NewType
#9808
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1409,12 +1409,13 @@ def __new__(self, typename, fields=None, **kwargs): | |
| " can be provided to NamedTuple, not both") | ||
| return _make_nmtuple(typename, fields) | ||
|
|
||
| class NewType: | ||
|
|
||
| def NewType(name, tp): | ||
| """NewType creates simple unique types with almost zero | ||
| runtime overhead. NewType(name, tp) is considered a subtype of tp | ||
| by static type checkers. At runtime, NewType(name, tp) returns | ||
| a dummy function that simply returns its argument. Usage:: | ||
| """NewType creates simple unique types with almost zero runtime | ||
| overhead. `NewType(name, tp)` is considered a subtype of `tp` | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't use backquotes in docstrings. They are not interpreted. |
||
| by static type checkers. At runtime, NewType(name, tp) creates | ||
| a callable instance that simply returns its argument when called. | ||
| Usage:: | ||
|
|
||
| UserId = NewType('UserId', int) | ||
|
|
||
|
|
@@ -1429,18 +1430,31 @@ def name_by_id(user_id: UserId) -> str: | |
| num = UserId(5) + 1 # type: int | ||
| """ | ||
|
fish2000 marked this conversation as resolved.
Outdated
|
||
|
|
||
| def new_type(x): | ||
| return x | ||
| __slots__ = ('__name__', '__qualname__', '__supertype__') | ||
|
|
||
| def __init__(self, name, tp): | ||
| self.__name__ = self.__qualname__ = name | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Setting |
||
| self.__supertype__ = tp | ||
|
|
||
| @staticmethod | ||
| def __call__(arg): | ||
| return arg | ||
|
|
||
| new_type.__name__ = name | ||
| new_type.__supertype__ = tp | ||
| return new_type | ||
| def __repr__(self): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would make the repr in the form def __repr__(self):
return f"{type(self).__name__}({self.__name__!r}, {self.__supertype__})" |
||
| """ NewType reprs are in the form: | ||
| “NewTClassName<typename:supertypename>”, e.g.: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please don't use non-ASCII quotes. Also I am not sure this docstirng is actually needed, the code is self-explanatory. |
||
| `NewType<UserId:int>` | ||
| """ | ||
| return f"{type(self).__name__}<" \ | ||
| f"{self.__qualname__}:" \ | ||
| f"{self.__supertype__.__name__}>" | ||
|
|
||
| def __hash__(self): | ||
| return hash((self.__name__, self.__supertype__)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this is needed?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have the same question. |
||
|
|
||
| # Python-version-specific alias (Python 2: unicode; Python 3: str) | ||
| Text = str | ||
|
|
||
|
|
||
| # Constant that's True when type checking, but False here. | ||
| TYPE_CHECKING = False | ||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Updated the `typing.NewType(…)` function to create real callable types – | ||
| This allows the generated types to furnish a real __repr__ function, while | ||
| retaining the same behavior (specifically, the near-zero runtime overhead). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't guarantee this.