Skip to content

Outer type wrongly replaced by inner method definition #14205

Closed as not planned
Closed as not planned
@Avasam

Description

@Avasam

Bug Report

Naming a method the same as an existing type will override existing type when later referenced.

To Reproduce

class MyClass:
    def type(self): ...
    def map(self): ...
    def foo(self, map: map, type: type): ...

Expected Behavior

map should be builtins.map, not MyClass.map and type should be builtins.type, not MyClass.type

Actual Behavior

method type issue.py:4: error: Function "method type issue.MyClass.map" is not valid as a type  [valid-type]
method type issue.py:4: note: Perhaps you need "Callable[...]" 
or a callback protocol?
method type issue.py:4: error: Function "method type issue.MyClass.type" is not valid as a type  [valid-type]
Found 2 errors in 1 file (checked 1 source file)

The type is taken from the class methods. If I wanted to use the class methods I'd do

class MyClass:
    def type(self): ...
    def map(self): ...
    def foo(self, map: 'MyClass.map', type: 'MyClass.type'): ...

(which aren't valid types anyway)
Of course I should use a different name for methods to begin with, but unfortunately I don't have control over that in a type stub.

Your Environment

  • Mypy version used: 0.990 (compiled: yes)
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.9.13

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