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

Turn typing.List, typing.Dict, etc into simple aliases of list, dict, etc #948

Closed
tmke8 opened this issue Nov 18, 2021 · 2 comments
Closed
Labels
topic: feature Discussions about new features for Python's type annotations

Comments

@tmke8
Copy link

tmke8 commented Nov 18, 2021

The recent proposal in python-ideas to rename classes in the standard library to have CamelCase names, reminded me that list, dict, tuple, set, frozenset already have CamelCase aliases in the typing module. However, they can't be used to instantiate anything:

>>> List(2*x for x in range(3))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/tmk/.conda/envs/palbolts/lib/python3.9/typing.py", line 675, in __call__
    raise TypeError(f"Type {self._name} cannot be instantiated; "
TypeError: Type List cannot be instantiated; use list() instead

But with PEP 585, there is no real reason to keep builtins.list distinct from typing.List, right? The CamelCase aliases in the typing module could just stay there indefinitely for people who want their classes to be in CamelCase.

You could maybe argue that this situation would be confusing to newcomers, but code from the era 3.6-3.8 is already littered with typing.List, typing.Dict, etc so it doesn't really make the situation worse.

There was discussion about this #367 where it was concluded that List[int]() should not be allowed, but that point is now kind of moot because since PEP 585, list[int]() is already allowed, so I don't see a good reason why List[int]() would be worse.

@tmke8 tmke8 added the topic: feature Discussions about new features for Python's type annotations label Nov 18, 2021
@sobolevn
Copy link
Member

sobolevn commented Nov 18, 2021

Important note: they are different right now:

>>> list[1]
list[1]
>>> from typing import List
>>> List[1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/sobolev/.pyenv/versions/3.9.1/lib/python3.9/typing.py", line 262, in inner
    return func(*args, **kwds)
  File "/Users/sobolev/.pyenv/versions/3.9.1/lib/python3.9/typing.py", line 814, in __getitem__
    params = tuple(_type_check(p, msg) for p in params)
  File "/Users/sobolev/.pyenv/versions/3.9.1/lib/python3.9/typing.py", line 814, in <genexpr>
    params = tuple(_type_check(p, msg) for p in params)
  File "/Users/sobolev/.pyenv/versions/3.9.1/lib/python3.9/typing.py", line 151, in _type_check
    raise TypeError(f"{msg} Got {arg!r:.100}.")
TypeError: Parameters to generic types must be types. Got 1.

@JelleZijlstra
Copy link
Member

I'd rather not do this. The point of typing.py isn't to help "people who want their classes to be in CamelCase", it's to provide helpers for type annotating your code. The camel case aliases are still there for compatibility but should eventually be removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: feature Discussions about new features for Python's type annotations
Projects
None yet
Development

No branches or pull requests

4 participants