Skip to content

[red-knot] False-positive errors for a sys.version_info-guarded import #17049

@AlexWaygood

Description

@AlexWaygood

Summary

If python-version is set to 3.9, red-knot emits an error on the following code:

import sys

if sys.version_info >= (3, 11):
    from typing import Self  # error: Module `typing` has no member `Self`

Red-knot correctly understands that the code is okay if python-version is set to 3.11 or higher. However, this kind of sys.version_info-guarded import is reasonably common in code that aims to support old Python versions -- e.g. lots of libraries do things like this, so that they can have typing_extensions only as a dependency on old Python versions:

import sys

if sys.version_info >= (3, 11):
    from typing import Self
else:
    from typing_extensions import Self

Mypy and pyright both support this pattern, so I think it's quite important for red-knot to also support it (though it's perhaps not something that's essential to do before the alpha release).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtyMulti-file analysis & type inference

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions