Skip to content

mypy should fail if the first element of the type has the same name as the variable #17227

Open
@userappgate

Description

@userappgate

This example file.

import pathlib

class A:
    pathlib: pathlib.Path = pathlib.Path('/')
$ mypy test.py 
Success: no issues found in 1 source file

$ python3 test.py 
Traceback (most recent call last):
  File "/tmp/test.py", line 3, in <module>
    class A:
  File "/tmp/test.py", line 4, in A
    pathlib: pathlib.Path = pathlib.Path('/')
             ^^^^^^^^^^^^
AttributeError: 'PosixPath' object has no attribute 'Path'. Did you mean: 'match'?

Expected Behavior

I would expect mypy to fail, possibly explaining why.

Actual Behavior

It succeeds, even though the type annotations cause a failure.

Your Environment

  • Mypy version used: mypy 1.10.0 (compiled: yes)
  • Python version used: Python 3.11.9

I had thought incorrectly that it was an issue with attrs python-attrs/attrs#1282 but @euresti provided the example to re-create the problem.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-runtime-semanticsmypy doesn't model runtime semantics correctly

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @JelleZijlstra@userappgate

        Issue actions

          mypy should fail if the first element of the type has the same name as the variable · Issue #17227 · python/mypy