Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-124412: Add helpers for converting annotations to source format#124551
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
f6161df7b6fb32430f184a3cb74c4ebc4064921cb2304e4fcFile 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 |
|---|---|---|
| @@ -242,21 +242,10 @@ def _type_repr(obj): | ||
| typically enough to uniquely identify a type. For everything | ||
| else, we fall back on repr(obj). | ||
| """ | ||
| # When changing this function, don't forget about | ||
| # `_collections_abc._type_repr`, which does the same thing | ||
| # and must be consistent with this one. | ||
| if isinstance(obj, type): | ||
| if obj.__module__ == 'builtins': | ||
| return obj.__qualname__ | ||
| return f'{obj.__module__}.{obj.__qualname__}' | ||
| if obj is ...: | ||
| return '...' | ||
| if isinstance(obj, types.FunctionType): | ||
| return obj.__name__ | ||
| if isinstance(obj, tuple): | ||
| # Special case for `repr` of types with `ParamSpec`: | ||
| return '[' + ', '.join(_type_repr(t) for t in obj) + ']' | ||
Comment on lines 245 to 247 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. This is a case we don't want in MemberAuthor 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. Yes, I think it's specifically for the internals of Callable and doesn't make sense in general. (Notice that it represents tuples with list syntax.) | ||
| return repr(obj) | ||
| return annotationlib.value_to_source(obj) | ||
| def _collect_type_parameters(args, *, enforce_default_ordering: bool = True): | ||
| @@ -2948,14 +2937,10 @@ def annotate(format): | ||
| if format in (annotationlib.Format.VALUE, annotationlib.Format.FORWARDREF): | ||
| return checked_types | ||
| else: | ||
| return _convert_to_source(types) | ||
| return annotationlib.annotations_to_source(types) | ||
| return annotate | ||
| def _convert_to_source(types): | ||
| return{n: t if isinstance(t, str) else _type_repr(t) for n, t in types.items()} | ||
| # attributes prohibited to set in NamedTuple class syntax | ||
| _prohibited = frozenset({'__new__', '__init__', '__slots__', '__getnewargs__', | ||
| '_fields', '_field_defaults', | ||
| @@ -3241,7 +3226,7 @@ def __annotate__(format): | ||
| for n, tp in own.items() | ||
| } | ||
| elif format == annotationlib.Format.SOURCE: | ||
| own = _convert_to_source(own_annotations) | ||
| own = annotationlib.annotations_to_source(own_annotations) | ||
| else: | ||
| own = own_checked_annotations | ||
| annos.update(own) | ||
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.
"approximately the SOURCE format" 😆