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

Skip to content

Outer type wrongly replaced by inner method definition #14205

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
Avasam opened this issue Nov 27, 2022 · 1 comment
Closed

Outer type wrongly replaced by inner method definition #14205

Avasam opened this issue Nov 27, 2022 · 1 comment
Labels
bug mypy got something wrong

Comments

@Avasam
Copy link
Contributor

Avasam commented Nov 27, 2022

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
@Avasam Avasam added the bug mypy got something wrong label Nov 27, 2022
@JelleZijlstra
Copy link
Member

Mypy is correct here and you'll see the same behavior at runtime. Possible workarounds:

  • import builtins and use builtins.map explicitly
  • from builtins import map as _map (or some other name) and use _map in annotations
  • Alias _map: TypeAlias = map in the global scope

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