Skip to content

Make typing.pyi container types alias builtins/collections #13589

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

Closed
wants to merge 2 commits into from

Conversation

yangdanny97
Copy link
Contributor

Since 3.9 is the minimum supported version (#12112), we could update these definitions in typing.pyi to be regular aliases since the builtin/collection versions support subscripting now.

Could this be done now? Or should we wait till april?

cc @ndmitchell

@JelleZijlstra
Copy link
Member

They aren't aliases at runtime, so type checkers might want to treat list and typing.List differently in some circumstances. For example, type checkers might narrow on if type(x) is list, but it would be incorrect to narrow on if type(x) is List.

@AlexWaygood
Copy link
Member

There are also other differences:

Python 3.11.4 (main, Sep 30 2023, 10:54:38) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from typing import List
>>> List()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.11/typing.py", line 1259, in __call__
    raise TypeError(f"Type {self._name} cannot be instantiated; "
TypeError: Type List cannot be instantiated; use list() instead
>>>

Copy link
Contributor

github-actions bot commented Mar 6, 2025

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

@ndmitchell
Copy link
Contributor

@AlexWaygood
Copy link
Member

Nonetheless, I'm pretty sure that mypy does differentiate between list and List in some places. And I'd prefer to have precise stubs here so that we can differentiate between the two symbols in red-knot, too

@yangdanny97 yangdanny97 closed this Mar 6, 2025
@yangdanny97 yangdanny97 deleted the typing_alias branch March 7, 2025 04:55
Sign up for free to 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.

4 participants