typeshed icon indicating copy to clipboard operation
typeshed copied to clipboard

Use PEP 570 syntax in stdlib

Open hauntsaninja opened this issue 7 months ago • 8 comments

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

hauntsaninja avatar Jan 05 '24 22:01 hauntsaninja

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Jan 05 '24 22:01 github-actions[bot]

Fixing the stubtest issue...

hauntsaninja avatar Jan 05 '24 23:01 hauntsaninja

stubtest PR here: https://github.com/python/mypy/pull/16750

hauntsaninja avatar Jan 06 '24 00:01 hauntsaninja

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?

AlexWaygood avatar Jan 06 '24 00:01 AlexWaygood

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

hauntsaninja avatar Jan 06 '24 01:01 hauntsaninja

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

github-actions[bot] avatar Jan 06 '24 01:01 github-actions[bot]

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

https://github.com/python/typeshed/blob/1de5830a2703936a96a126248227d5c7db883674/stdlib/builtins.pyi#L204-L205

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

AlexWaygood avatar Jan 06 '24 13:01 AlexWaygood

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

hauntsaninja avatar Jan 07 '24 00:01 hauntsaninja

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

github-actions[bot] avatar Mar 09 '24 22:03 github-actions[bot]