Skip to content

Conditionally excluding backward incompatible syntax based on python version #19134

Open
@alisaifee

Description

@alisaifee

Feature

There doesn't seem to be a way to progressively provide improvements in libraries that support older versions with syntactically backward incompatible code that is conditionally included at runtime.

For example in pythons < 3.12 it's not possible to exclude certain files containing PEP 695 type syntax even if the module is only imported with a sys.version_info >= (3,12) guard.

import sys

if sys.version_info >= (3, 12):
    from _py312_types import A
else:
    from _types import A

__all__ = ["A"]
# file: _py312_types.py
type A = list[int|A]
# file: _types.py
A = list[int|list["A"]]

Pitch

The only existing path that I could find was to allow python_version overrides on a per module basis (which is currently only acceptable as a global configuration) though that seems hacky and brittle.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions