Skip to content

no-redef when import-importing a module to satisfy a protocol #13803

Closed
@asottile

Description

@asottile

Bug Report

modules-as-protocols triggers "no-redef" when using import-imports, but is fine with from-imports

(I'm testing out this unreleased feature on the primary branch)

# main.py
import os
from typing import Protocol

class P(Protocol):
    def f(self) -> str: ...

t: P
if os.environ.get('ENV') == 'prod':
    import t as t  # Name "t" already defined on line 7  [no-redef]
else:
    import u as t  # Name "t" already defined on line 7  [no-redef]
# t.py and u.py
def f() -> str:
    return ''

the errors triggered are inline above

oddly enough, a from import seems to work fine (so there's an easy workaround if the module isn't top-level):

mkdir y
touch y/__init__.py
mv {t,u}.py y
import os
from typing import Protocol

class P(Protocol):
    def f(self) -> str: ...

t: P
if os.environ.get('ENV') == 'prod':
    from y import t as t  # no errors!
else:
    from y import u as t  # no errors!

Your Environment

  • Mypy version used: mypy 0.990+dev.dc5c299aa190949f2300b163ccc10257a779006d (compiled: no)
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.10.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions