Thanks to visit codestin.com
Credit goes to github.com

Skip to content

type[] inside a class with an attribute named type gives an error #14245

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
LefterisJP opened this issue Dec 4, 2022 · 3 comments
Closed

type[] inside a class with an attribute named type gives an error #14245

LefterisJP opened this issue Dec 4, 2022 · 3 comments
Labels
bug mypy got something wrong

Comments

@LefterisJP
Copy link
Contributor

Bug Report

Using type[] instead of Type[] in a class with an attribute named type gives an error.

To Reproduce

Consider the following:

from typing import TypeVar

T = TypeVar('T', bound='Foo')

class Foo:
    type: str

    @classmethod
    def foo(cls: type[T], value: str) -> None:
        pass

https://gist.github.com/mypy-play/da5ef553b28dee4ff64e6bac829ab260
https://mypy-play.net/?mypy=0.991&python=3.9&gist=da5ef553b28dee4ff64e6bac829ab260

Expected Behavior

I would expect to not see any error. Same as I do when I import Type from typing and use that instead. Then there is no error.

Actual Behavior

test4.py:9: error: Variable "test4.Foo.type" is not valid as a type  [valid-type]
test4.py:9: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
Found 1 error in 1 file (checked 1 source file)

I am sure it's something to do with the class using a reserved keyword for its attribute and that somehow clashing with the type keyword of typing.

As I said above if you replace type with typing.Type it all works fine.

Your Environment

  • Mypy version used: 0.991
  • Mypy command-line flags: --install-types --non-interactive
  • Mypy configuration options from mypy.ini (and other config files):
[tool.mypy]
# Docs: https://mypy.readthedocs.io/en/latest/config_file.html
ignore_missing_imports = true
check_untyped_defs = true
disallow_untyped_defs = true
warn_unused_configs = true
warn_unused_ignores = true
warn_unreachable = true
warn_redundant_casts = true
disallow_untyped_decorators = true
disallow_untyped_calls = true
mypy_path="./stubs/"
  • Python version used: Python 3.9.10
@LefterisJP LefterisJP added the bug mypy got something wrong label Dec 4, 2022
@AlexWaygood
Copy link
Member

This is correct behaviour from mypy (duplicate of #14205).

Possible workarounds are:

  • Use typing.Type, use Type[] for your annotation
  • Stick import builtins at the top, use builtins.type[] for your annotation
  • Add from builtins import type as Type at the top, use Type[] for your annotation

@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Dec 4, 2022
@LefterisJP
Copy link
Contributor Author

Thanks @AlexWaygood! Perhaps we should add this exact example of workarounds in the docs? Do you think it would make sense to open a PR for this as I guess I am not the only person hitting this.

@AlexWaygood
Copy link
Member

Yeah, I wondered about that. It seems like it'll crop up more and more as more people switch to PEP 585 syntax in their projects. It might make a good addition to this page: https://mypy.readthedocs.io/en/stable/common_issues.html

LefterisJP added a commit to LefterisJP/mypy that referenced this issue Dec 4, 2022
As discussed with @AlexWaygood in
python#14245 (comment)
adding a common issue entry for the problem of hitting overshadowing a
built-in keyword when upgrading to PEP584 syntax
LefterisJP added a commit to LefterisJP/mypy that referenced this issue Dec 4, 2022
As discussed with @AlexWaygood in
python#14245 (comment)
adding a common issue entry for the problem of hitting overshadowing a
built-in keyword when upgrading to PEP584 syntax
LefterisJP added a commit to LefterisJP/mypy that referenced this issue Dec 4, 2022
As discussed with @AlexWaygood in
python#14245 (comment)
adding a common issue entry for the problem of hitting overshadowing a
built-in keyword when upgrading to PEP585 syntax
LefterisJP added a commit to LefterisJP/mypy that referenced this issue Dec 4, 2022
As discussed with @AlexWaygood in
python#14245 (comment)
adding a common issue entry for the problem of hitting overshadowing a
built-in keyword when upgrading to PEP585 syntax
LefterisJP added a commit to LefterisJP/mypy that referenced this issue Dec 4, 2022
As discussed with @AlexWaygood in
python#14245 (comment)
adding a common issue entry for the problem of hitting overshadowing a
built-in keyword when upgrading to PEP585 syntax
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants