Skip to content

Don't issue no-redef with conditional imports #13914

Open

Description

@bersbersbers

Bug Report

When an exception is caught from a block with a single import statement, one can be certain that the import failed (right?).

When an import fails, a to-be-imported function is not defined.

If a function is not defined, mypy should not issue a no-redef message.

To Reproduce

pip install overrides

"""Feature request: detect failed imports and suppress no-redef."""
try:
    from overrides import overrides
except ModuleNotFoundError:
    # mypy issues no-redef
    def overrides() -> None:
        pass

print(overrides)

Expected Behavior

No message

Actual Behavior

bug.py:5: error: Name "overrides" already defined (possibly by an import) [no-redef]
Found 1 error in 1 file (checked 1 source file)

Your Environment

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

Activity

  1. bersbersbers commented on Oct 17, 2022

    @bersbersbers
    Author
  2. bersbersbers commented on Oct 17, 2022

    @bersbersbers
    Author

    A workaround can be found in mkorpela/overrides#109 (comment).

  3. added a commit that references this issue on Nov 3, 2022
    a4da89e
  4. bersbersbers commented on Nov 3, 2022

    @bersbersbers
    Author

    @hauntsaninja I may be missing something, but the code example I posted in #13914 (comment) still issues the same mypy error:

    C:\Code\project>mypy --version
    mypy 1.0.0+dev.a4da89e0543ee0d213b30216c9e15a5d00fcd578 (compiled: no)
    
    C:\Code\project>mypy --no-incremental project\project.py
    project\project.py:6: error: Name "overrides" already defined (possibly by an import)  [no-redef]
    Found 1 error in 1 file (checked 1 source file)
    

    Using overrides==7.3.1

  5. bersbersbers commented on Feb 7, 2023

    @bersbersbers
    Author

    Just for the record (because I had been hoping it might have been fixed), this is still an issue on 1.0.0.

  6. bersbersbers commented on Feb 12, 2023

    @bersbersbers
    Author

    Some more details:

    """With `overrides==7.3.1` installed, mypy issues `no-redef` in `except`."""
    from typing import Callable, TypeVar
    
    F = TypeVar("F", bound=Callable)
    try:
        from overrides import override
    except ModuleNotFoundError:
        def override(method: F) -> F:
            return method
    
    print(override)

    The workaround works , but stops working when removing overrides:

    """The workaround silences `mypy`, but now mypy issues `no-redef` in *`try`* if `overrides` is *not* installed."""
    from typing import Callable, TypeVar
    
    F = TypeVar("F", bound=Callable)
    
    # Workaround
    override: Callable[[F], F]
    try:
        from overrides import override
    except ModuleNotFoundError:
        def override(method: F) -> F:
            return method
    
    print(override)

    This is a problem if one uses a construct such as this to import typing.override (from Python 3.12 on):

    """Prepare for Python 3.12."""
    from typing import Callable, TypeVar
    
    F = TypeVar("F", bound=Callable)
    
    # Workaround
    override: Callable[[F], F]
    try:
        from typing import override
    except ModuleNotFoundError:
        def override(method: F) -> F:
            return method
    
    print(override)

    For the record, this works:

    """Prepare for Python 3.12."""
    import sys
    from typing import Callable, TypeVar
    
    F = TypeVar("F", bound=Callable)
    
    if sys.version_info >= (3, 12):
        from typing import override
    else:
        def override(method: F) -> F:
            return method
    
    print(override)
  7. added a commit that references this issue on Jan 1, 2025
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 wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions