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

type class cannot be used as a generic #11424

Closed
ghost opened this issue Nov 1, 2021 · 8 comments
Closed

type class cannot be used as a generic #11424

ghost opened this issue Nov 1, 2021 · 8 comments
Labels
bug mypy got something wrong topic-pep-585 PEP 585 (builtin generics)

Comments

@ghost
Copy link

ghost commented Nov 1, 2021

(moved from python/typeshed#6218)

It seems as of python 3.9, type[...] has a special case for returning GenericAlias while not having a __class_getitem__, this causes issues when using it in favour of typing.Type:

$ python -c 'type[int]'  # Works
$ mypy -c 'type[int]'
<string>:1: error: Value of type "Type[type]" is not indexable
Found 1 error in 1 file (checked 1 source file)

I stumbled upon this issue with python 3.9.7/mypy 0.910 but it is still there as of mypy 0.920+dev.8b9ade2

@ghost ghost added the bug mypy got something wrong label Nov 1, 2021
@ghost ghost changed the title type object cannot be used as a generic type class cannot be used as a generic Nov 1, 2021
@hauntsaninja
Copy link
Collaborator

Oh, interesting. The following do work on master:

λ mypy -c 'z: type[int]' --python-version 3.9 
Success: no issues found in 1 source file
λ mypy -c 'z = type[int]' --python-version 3.9
Success: no issues found in 1 source file

Clearly a bug, curious if you have a more complex example that fails on master as well?

@ghost
Copy link
Author

ghost commented Nov 1, 2021

Hmm, this is weird:

$ python -m mypy --version
mypy 0.920+dev.0ec868fe778c57b52f6df54f54eb19616e1d6ba2
$ python -m mypy -c 'type[int]' --python-version 3.9 
<string>:1: error: Value of type "Type[type]" is not indexable
Found 1 error in 1 file (checked 1 source file)

$ python -m mypy -c 'foo: type[int]' --python-version 3.9 
Success: no issues found in 1 source file

$ python -m mypy -c 'reveal_type(type[int]); foo: type[int]; reveal_type(foo)' --python-version 3.9 
<string>:1: error: Value of type "Type[type]" is not indexable
<string>:1: note: Revealed type is "Any"
<string>:1: note: Revealed type is "Type[builtins.int]"
Found 1 error in 1 file (checked 1 source file)

I also can't seem to reproduce the issue I was having, which was using something like def foo(cls: type[T]) -> T (or, I forgot some detail).. I guess this is low priority then?

@AlexWaygood
Copy link
Member

AlexWaygood commented Mar 31, 2022

No longer reproducible on 0.942

@mthuurne
Copy link
Contributor

mthuurne commented Jun 1, 2022

I can still reproduce this:

$ mypy --python-version 3.10 -c 'type[int]'
<string>:1: error: Value of type "Type[type]" is not indexable  [index]
Found 1 error in 1 file (checked 1 source file)
$ mypy --version
mypy 0.950 (compiled: yes)

And it also reproduces for me on 0.942.

It might be a duplicate of #12392 though.

@AlexWaygood
Copy link
Member

I can still reproduce this:

$ mypy --python-version 3.10 -c 'type[int]'
<string>:1: error: Value of type "Type[type]" is not indexable  [index]
Found 1 error in 1 file (checked 1 source file)
$ mypy --version
mypy 0.950 (compiled: yes)

And it also reproduces for me on 0.942.

It might be a duplicate of #12392 though.

Thanks, you're right -- not sure why I closed this. As you say, though, there's heavy overlap with #12392, which has more practical use cases in it. So I don't think there's a need to reopen, personally.

@mthuurne
Copy link
Contributor

I can still reproduce the issue even after the #12392 fix: it seems that type[X] is handled when it occurs in an annotation or a potential alias, but not when it's used as any other kind of expression:

def f() -> type:
    a: type[int]  # fine
    A = type[int]  # fine
    return type[int]  # error: Value of type "type[type]" is not indexable  [index]

I'm using mypy 1.4.0 on Python 3.10.6.

Could this issue be re-opened?

@AlexWaygood AlexWaygood added the topic-pep-585 PEP 585 (builtin generics) label Jun 22, 2023
@AlexWaygood
Copy link
Member

I can still reproduce the issue even after the #12392 fix: it seems that type[X] is handled when it occurs in an annotation or a potential alias, but not when it's used as any other kind of expression:

def f() -> type:
    a: type[int]  # fine
    A = type[int]  # fine
    return type[int]  # error: Value of type "type[type]" is not indexable  [index]

I'm using mypy 1.4.0 on Python 3.10.6.

Could this issue be re-opened?

This is basically covered by #13026 I think

@mthuurne
Copy link
Contributor

Agreed, no need to re-open then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-pep-585 PEP 585 (builtin generics)
Projects
None yet
Development

No branches or pull requests

3 participants