Skip to content
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

Use _typeshed.Self in Python 2, too #6932

Merged
merged 5 commits into from
Jan 16, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion stdlib/typing_extensions.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class _TypedDict(Mapping[str, object], metaclass=abc.ABCMeta):
__required_keys__: frozenset[str]
__optional_keys__: frozenset[str]
__total__: bool
def copy(self: _T) -> _T: ...
def copy(self: _T) -> _T: ... # noqa: Y019
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't this use Self?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of line 45 — typing_extensions.Self is a _SpecialForm, not a TypeVar. I deliberately excluded typing_extensions from my script for that reason.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use from _typeshed import Self as _Self?

Copy link
Member Author

@AlexWaygood AlexWaygood Jan 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd still need a # noqa comment, though, because the id of the ast.Name node used for the annotation would be _Self rather than Self. PyCQA/flake8-pyi#90 hardcodes it as having to be Self.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could do from _typeshed import Self as TypeshedSelf and it would pass without a # noqa — my proposed Y019 only raises errors for names starting with a leading underscore.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do that, that will also make it obvious that we can switch to PEP 673 Self when that day comes.

# Using NoReturn so that only calls using mypy plugin hook that specialize the signature
# can go through.
def setdefault(self, k: NoReturn, default: object) -> object: ...
Expand Down