Skip to content

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

Closed

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

Activity

  1. hauntsaninja commented on Oct 4, 2022

    @hauntsaninja
    Collaborator

    Thanks for the report!

    Note mainly for self: looks like the difference is between add_symbol_table_node

    if (

    and process_imported_symbol
    existing_symbol = self.globals.get(imported_id)
    . It's not obvious to me why process_import_over_existing_name isn't called for normal imports, but just changing that should probably fix

  2. added a commit that references this issue on Oct 31, 2022
    a7915af
  3. added a commit that references this issue on Nov 3, 2022
    a4da89e
  4. hauntsaninja commented on Nov 3, 2022

    @hauntsaninja
    Collaborator

    Thanks for reporting! This should now be fixed on master, but isn't part of the 0.990 release.

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions