Skip to content

Use PEP 570 syntax in stdlib - #11250

Merged
hauntsaninja merged 2 commits into
python:mainfrom
hauntsaninja:pep570-stdlib
Mar 9, 2024
Merged

Use PEP 570 syntax in stdlib#11250
hauntsaninja merged 2 commits into
python:mainfrom
hauntsaninja:pep570-stdlib

Conversation

@hauntsaninja

@hauntsaninjahauntsaninja commented Jan 5, 2024

Copy link
Copy Markdown
Collaborator

Fixes#11237

This is the script I used: https://raw.githubusercontent.com/hauntsaninja/snippets/main/pep570.py

I haven't looked through the entire diff, let's see stubtest

@hauntsaninjahauntsaninja changed the title Use PPE 570 syntax in stdlibUse PEP 570 syntax in stdlibJan 5, 2024
@hauntsaninja
hauntsaninja marked this pull request as draft January 5, 2024 22:16
@github-actions

This comment has been minimized.

@hauntsaninja

Copy link
Copy Markdown
CollaboratorAuthor

Fixing the stubtest issue...

@hauntsaninja

Copy link
Copy Markdown
CollaboratorAuthor

stubtest PR here: python/mypy#16750

@AlexWaygood

Copy link
Copy Markdown
Member

This is the script I used: https://raw.githubusercontent.com/hauntsaninja/snippets/main/pep570.py

Looks reasonable, though I wonder if it handles classmethods? You could probably special-case parameters named cls (and maybe even mcls, for metaclass classmethods?) in the same way you special-case parameters named self, right?

@hauntsaninja

Copy link
Copy Markdown
CollaboratorAuthor

Ah did I not push that change? Oops, looks like I did not. Good point on metaclasses, will do that as well

@github-actions

This comment has been minimized.

@AlexWaygood

Copy link
Copy Markdown
Member

Looks like there's only one instance of the old syntax that PyCQA/flake8-pyi#461 detects in the stdlib and this PR doesn't fix:

@classmethod
def__prepare__(metacls, __name: str, __bases: tuple[type, ...], **kwds: Any) ->MutableMapping[str, object]: ...

You could fix that as well by also special-casing parameters named metacls in the script (or just fix it manually with an additional commit).

Maybe it'd be best to revert the handful of changes stubtest has issues with for now? We can easily defer them until there's a mypy release with the stubtest fix

@hauntsaninja

hauntsaninja commented Jan 7, 2024

Copy link
Copy Markdown
CollaboratorAuthor

Thanks for checking that! I also found several cases of incorrect positional-only arguments that this PR leaves alone.

It's basically all overloads containing positional-only args. Since this isn't user facing, I'd rather either just wait or use a pre-release mypy for stubtest CI

@github-actions

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

prefect (https://github.com/PrefectHQ/prefect)
- src/prefect/utilities/pydantic.py:184: note: def __new__(cls: type[type], object, /) -> type+ src/prefect/utilities/pydantic.py:184: note: def __new__(type[type], object, /) -> type- src/prefect/utilities/pydantic.py:184: note: def [_typeshed.Self] __new__(cls: type[_typeshed.Self], str, tuple[type, ...], dict[str, Any], /, **kwds: Any) -> _typeshed.Self+ src/prefect/utilities/pydantic.py:184: note: def [_typeshed.Self] __new__(type[_typeshed.Self], str, tuple[type, ...], dict[str, Any], /, **kwds: Any) -> _typeshed.Self- src/prefect/utilities/pydantic.py:186: note: def __new__(cls: type[type], object, /) -> type+ src/prefect/utilities/pydantic.py:186: note: def __new__(type[type], object, /) -> type- src/prefect/utilities/pydantic.py:186: note: def [_typeshed.Self] __new__(cls: type[_typeshed.Self], str, tuple[type, ...], dict[str, Any], /, **kwds: Any) -> _typeshed.Self+ src/prefect/utilities/pydantic.py:186: note: def [_typeshed.Self] __new__(type[_typeshed.Self], str, tuple[type, ...], dict[str, Any], /, **kwds: Any) -> _typeshed.Self

@hauntsaninja
hauntsaninja marked this pull request as ready for review March 9, 2024 22:47
@hauntsaninjahauntsaninja removed the status: deferred Issue or PR deferred until some precondition is fixed label Mar 9, 2024

@JelleZijlstraJelleZijlstra left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looked at a couple of files, looks great.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Switch to PEP 570 positional-only argument syntax

3 participants

@hauntsaninja@AlexWaygood@JelleZijlstra