Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
bpo-38605: Make postponed evaluation of annotations default#20434
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
4c5b31434028779a131a2fdcea4d14e7c2df217d486209fa0bbf01bb246577d1d779176bd5fee1e2c3f80a04da86d1feb586181e442762845ae4bd11e73aa7File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -398,8 +398,10 @@ def _create_fn(name, args, body, *, globals=None, locals=None, | ||
| ns ={} | ||
| exec(txt, globals, ns) | ||
| return ns['__create_fn__'](**locals) | ||
| func = ns['__create_fn__'](**locals) | ||
| for arg, annotation in func.__annotations__.copy().items(): | ||
| func.__annotations__[arg] = locals[annotation] | ||
| return func | ||
| def _field_assign(frozen, name, value, self_name): | ||
| # If we're a frozen class, then assign to our fields in __init__ | ||
| @@ -650,6 +652,11 @@ def _is_type(annotation, cls, a_module, a_type, is_type_predicate): | ||
| # a eval() penalty for every single field of every dataclass | ||
| # that's defined. It was judged not worth it. | ||
| # Strip away the extra quotes as a result of double-stringifying when the | ||
| # 'annotations' feature became default. | ||
| if annotation.startswith(("'", '"')) and annotation.endswith(("'", '"')): | ||
| annotation = annotation[1:-1] | ||
| match = _MODULE_IDENTIFIER_RE.match(annotation) | ||
| if match: | ||
| ns = None | ||
| @@ -990,7 +997,7 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen): | ||
| if not getattr(cls, '__doc__'): | ||
| # Create a class doc-string. | ||
| cls.__doc__ = (cls.__name__ + | ||
| str(inspect.signature(cls)).replace(' -> None', '')) | ||
| str(inspect.signature(cls)).replace(' -> NoneType', '')) | ||
gvanrossum marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| return cls | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -45,6 +45,7 @@ | ||
| import tokenize | ||
| import token | ||
| import types | ||
| import typing | ||
| import warnings | ||
| import functools | ||
| import builtins | ||
| @@ -1877,7 +1878,10 @@ def _signature_is_functionlike(obj): | ||
| code = getattr(obj, '__code__', None) | ||
| defaults = getattr(obj, '__defaults__', _void) # Important to use _void ... | ||
| kwdefaults = getattr(obj, '__kwdefaults__', _void) # ... and not None here | ||
| annotations = getattr(obj, '__annotations__', None) | ||
| try: | ||
| annotations = _get_type_hints(obj) | ||
| except AttributeError: | ||
| annotations = None | ||
| return (isinstance(code, types.CodeType) and | ||
| isinstance(name, str) and | ||
| @@ -2118,6 +2122,16 @@ def p(name_node, default_node, default=empty): | ||
| return cls(parameters, return_annotation=cls.empty) | ||
| def _get_type_hints(func): | ||
| try: | ||
| return typing.get_type_hints(func) | ||
| except Exception: | ||
| # First, try to use the get_type_hints to resolve | ||
| # annotations. But for keeping the behavior intact | ||
| # if there was a problem with that (like the namespace | ||
| # can't resolve some annotation) continue to use | ||
| # string annotations | ||
| return func.__annotations__ | ||
isidentical marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| def _signature_from_builtin(cls, func, skip_bound_arg=True): | ||
| """Private helper function to get signature for | ||
| @@ -2161,7 +2175,8 @@ def _signature_from_function(cls, func, skip_bound_arg=True): | ||
| positional = arg_names[:pos_count] | ||
| keyword_only_count = func_code.co_kwonlyargcount | ||
| keyword_only = arg_names[pos_count:pos_count + keyword_only_count] | ||
| annotations = func.__annotations__ | ||
| annotations = _get_type_hints(func) | ||
| defaults = func.__defaults__ | ||
| kwdefaults = func.__kwdefaults__ | ||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,3 @@ | ||
| from __future__ import annotations | ||
| import dataclasses | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.