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

Closed as not planned
@LefterisJP

Description

@LefterisJP

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions