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

PEP 585 (built-in generics) tracker #4820

Closed
8 tasks done
srittau opened this issue Dec 14, 2020 · 24 comments
Closed
8 tasks done

PEP 585 (built-in generics) tracker #4820

srittau opened this issue Dec 14, 2020 · 24 comments
Labels
project: feature tracker Tracks whether a typing feature can be used in typeshed stubs

Comments

@srittau
Copy link
Collaborator

srittau commented Dec 14, 2020

This issue is supposed to track when we can start to use builtin generics (PEP 585) in typeshed. Support is needed in released versions of:

Also, type aliases are a problem, although this might need TypeAlias support (#4913).

@srittau srittau added the project: feature tracker Tracks whether a typing feature can be used in typeshed stubs label Jan 6, 2021
@srittau
Copy link
Collaborator Author

srittau commented Jan 6, 2021

@rchen152 @mrkmndz Do pytype/pyre support PEP 585? If not, would you mind me creating a ticket in the respective projects that we can reference from here?

@sproshev
Copy link
Contributor

sproshev commented Jan 6, 2021

Supported by PyCharm since 2020.2 https://youtrack.jetbrains.com/issue/PY-42418

@rchen152
Copy link
Collaborator

rchen152 commented Jan 7, 2021

Internally, pytype has actually never distinguished between builtins.list and typing.List, etc. So we should already support this one.

@srittau
Copy link
Collaborator Author

srittau commented Jan 25, 2021

Test case:

from __future__ import annotations

from typing import List

list1: List[str] = []
list2: list[str] = []

reveal_type(list1)
reveal_type(list2)

pyre returns:

src/pep585.py:8:0 Revealed type [-1]: Revealed type for `list1` is `List[str]`.
src/pep585.py:9:0 Revealed type [-1]: Revealed type for `list2` is `List[str]`.

🎉 This means all type checkers now support this feature and we can start using it in typeshed. 🎉

@srittau srittau closed this as completed Jan 25, 2021
@Akuli
Copy link
Collaborator

Akuli commented Jan 25, 2021

Are we going to have a huge pull request to change List to list? Or is this not important enough for a huge pull request?

@srittau
Copy link
Collaborator Author

srittau commented Jan 25, 2021

We should require future PRs to use built-in generics. I am personally in favor of a "big bang" PR after the reshuffling is done. Currently is a good time, since we have nearly no open PRs that could cause conflicts. But I am curious what the other maintainers think.

@Hultner
Copy link

Hultner commented Jan 25, 2021

Are we going to have a huge pull request to change List to list? Or is this not important enough for a huge pull request?

How would that turn out for people still using python 3.7 or 3.8? Or should they just pin an older typeshed version?

@JelleZijlstra
Copy link
Member

I believe mypy actually does not yet support this in type aliases, only lists (x = list[int] doesn't work yet in mypy).

@Hultner this syntax should work in stubs even when it doesn't work at runtime yet. We should confirm that the type checkers actually allow this syntax in stubs when targeting older versions though.

@srittau
Copy link
Collaborator Author

srittau commented Feb 1, 2021

@JelleZijlstra A quick test seems to work:

alias1 = "List[str]"
alias2 = List[str]
alias3 = "list[str]"
alias4 = list[str]

def foo(x: alias4):
    for i in x:
        reveal_type(i)

Correctly reveals str for both alias2 and alias4. Both alias1 and alias2 don't work, though.

@srittau
Copy link
Collaborator Author

srittau commented Feb 23, 2021

Reopening because of python/mypy#9980.

@srittau srittau reopened this Feb 23, 2021
srittau added a commit to srittau/typeshed that referenced this issue Feb 23, 2021
JelleZijlstra pushed a commit that referenced this issue Feb 24, 2021
* Remove conventions enforced by black

Remove old note about optional default arguments (now part of
PEP 484 and enforced by CI)

* Recommend to use PEP 585

Cf. #4820

* Try out using collections.abc

* Reference mypy bug
@srittau
Copy link
Collaborator Author

srittau commented Apr 9, 2021

The following does not work with mypy:

@srittau
Copy link
Collaborator Author

srittau commented Jun 28, 2021

Base classes don't work either. python/mypy#10731

@JukkaL
Copy link
Contributor

JukkaL commented Jul 15, 2021

Mypy should be fixed on GitHub master. The fixes haven't been released yet, though.

@JelleZijlstra
Copy link
Member

With the latest mypy release this should be mostly fixed, but as I recall there's still a few issues.

@AlexWaygood
Copy link
Member

With the latest mypy release this should be mostly fixed, but as I recall there's still a few issues.

I am working on a PR for this.

@srittau
Copy link
Collaborator Author

srittau commented Dec 22, 2021

Generic type didn't work yet when I tried to use it. Tuples and base classes are fixed.

@sobolevn
Copy link
Member

@srittau do you have some opened issue to link me to? I can probably help with this one 🙂

Btw, this works for me on mypy@master:

def func(a: type[int]):
    reveal_type(a)  # N: Revealed type is "Type[builtins.int]"

func(int)  # ok
func(str)  # E: Argument 1 to "func" has incompatible type "Type[str]"; expected "Type[int]"

@srittau
Copy link
Collaborator Author

srittau commented Dec 22, 2021

At least the following still fails with mypy 0.920. I didn't test mypy master:

class Foo(type[int]):  # invalid base class "type"
    pass

@srittau
Copy link
Collaborator Author

srittau commented Dec 22, 2021

And here are a bunch of further errors: https://github.com/python/typeshed/runs/4604434037?check_suite_focus=true

#4820 for experiments.

@JukkaL
Copy link
Contributor

JukkaL commented Dec 22, 2021

Can you create mypy issues about any remaining issues and cc me? We can make a mypy point release with the fixes once everything works correctly (there should be no need to wait for a full release).

@srittau
Copy link
Collaborator Author

srittau commented Dec 22, 2021

python/mypy#10303 seems to be still problematic, but the errors from #4820 are gone with mypy 0.930. 🥳

@AlexWaygood
Copy link
Member

Many pytype errors were output by the CI when I tried to use imports from collections.abc everywhere 😕https://github.com/python/typeshed/runs/4629393978?check_suite_focus=true

@AlexWaygood
Copy link
Member

Many pytype errors were output by the CI when I tried to use imports from collections.abc everywhere 😕https://github.com/python/typeshed/runs/4629393978?check_suite_focus=true

I have opened google/pytype#1096 for the pytype errors.

@srittau
Copy link
Collaborator Author

srittau commented Feb 2, 2022

As of mypy 0.931 this is now fully supported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
project: feature tracker Tracks whether a typing feature can be used in typeshed stubs
Projects
None yet
Development

No branches or pull requests

9 participants