Mypy 1.14 Released (original) (raw)

We’ve just uploaded mypy 1.14 to the Python Package Index (PyPI).

Mypy is a static type checker for Python. This release includes new features and bug fixes.

You can install it as follows:

python3 -m pip install -U mypy

You can read the full documentation for this release on Read the Docs.

Change to Enum Membership Semantics

As per the updated typing specification for enums, enum members must be left unannotated.

class Pet(Enum):
    CAT = 1  # Member attribute
    DOG = 2  # Member attribute

    # New error: Enum members must be left unannotated
    WOLF: int = 3

    species: str  # Considered a non-member attribute

In particular, the specification change can result in issues in type stubs (.pyi files), since historically it was common to leave the value absent:

# In a type stub (.pyi file)

class Pet(Enum):
    # Change in semantics: previously considered members,
    # now non-member attributes
    CAT: int
    DOG: int

    # Mypy will now issue a warning if it detects this
    # situation in type stubs:
    # > Detected enum "Pet" in a type stub with zero
    # > members. There is a chance this is due to a recent
    # > change in the semantics of enum membership. If so,
    # > use member = value to mark an enum member,
    # > instead of member: type

class Pet(Enum):
    # As per the specification, you should now do one of
    # the following:
    DOG = 1  # Member attribute with value 1 and known type
    WOLF = cast(int, ...)  # Member attribute with unknown
                           # value but known type
    LION = ...  # Member attribute with unknown value and
                # unknown type

Contributed by Terence Honles (PR 17207) and

Shantanu Jain (PR 18068).

Support for @deprecated Decorator (PEP 702)

Mypy can now issue errors or notes when code imports a deprecated feature explicitly with a from mod import depr statement, or uses a deprecated feature imported otherwise or defined locally. Features are considered deprecated when decorated with warnings.deprecated, as specified in PEP 702.

You can enable the error code via --enable-error-code=deprecated on the mypy command line or enable_error_code = deprecated in the mypy config file.

Use the command line flag --report-deprecated-as-note or config file optionreport_deprecated_as_note=True to turn all such errors into notes.

Deprecation errors will be enabled by default in a future mypy version.

This feature was contributed by Christoph Tyralla.

List of changes:

Optionally Analyzing Untyped Modules

Mypy normally doesn't analyze imports from third-party modules (installed using pip, for example) if there are no stubs or a py.typed marker file. To force mypy to analyze these imports, you can now use the --follow-untyped-imports flag or set the follow_untyped_importsconfig file option to True. This can be set either in the global section of your mypy config file, or individually on a per-module basis.

This feature was contributed by Jannick Kremer.

List of changes:

Support New Style Type Variable Defaults (PEP 696)

Mypy now supports type variable defaults using the new syntax described in PEP 696, which was introduced in Python 3.13. Example:

@dataclass
class Box[T = int]:  # Set default for "T"
    value: T | None = None

reveal_type(Box())                      # type is Box[int], since it's the default
reveal_type(Box(value="Hello World!"))  # type is Box[str]

This feature was contributed by Marc Mueller (PR 17985).

Improved For Loop Index Variable Type Narrowing

Mypy now preserves the literal type of for loop index variables, to support TypedDictlookups. Example:

from typing import TypedDict

class X(TypedDict):
    hourly: int
    daily: int

def func(x: X) -> int:
    s = 0
    for var in ("hourly", "daily"):
        # "Union[Literal['hourly']?, Literal['daily']?]"
        reveal_type(var)

        # x[var] no longer triggers a literal-required error
        s += x[var]
    return s

This was contributed by Marc Mueller (PR 18014).

Mypyc Improvements

Stubgen Improvements

Stubtest Improvements

Documentation Updates

Other Notables Fixes and Improvements

Acknowledgements

Thanks to all mypy contributors who contributed to this release:

I’d also like to thank my employer, Dropbox, for supporting mypy development.