The Mypy Blog (original) (raw)

We’ve just uploaded mypy 1.16 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.

Different Property Getter and Setter Types

Mypy now supports using different types for a property getter and setter:

class A:
    _value: int

    @property
    def foo(self) -> int:
        return self._value

    @foo.setter
    def foo(self, x: str | int) -> None:
        try:
            self._value = int(x)
        except ValueError:
            raise Exception(f"'{x}' not valid value for 'foo'")

This was contributed by Ivan Levkivskyi (PR 18510).

Flexible Variable Redefinitions (Experimental)

Mypy now allows unannotated variables to be freely redefined with different types when using the experimental --allow-redefinition-newflag. You will also need to enable --local-partial-types. Mypy will now infer a union type when different types are assigned to a variable:

# mypy: allow-redefinition-new, local-partial-types

def f(n: int, b: bool) -> int | str:
    if b:
        x = n
    else:
        x = str(n)
    # Type of 'x' is int | str here.
    return x

Without the new flag, mypy only supports inferring optional types (X | None) from multiple assignments, but now mypy can infer arbitrary union types.

An unannotated variable can now also have different types in different code locations:

# mypy: allow-redefinition-new, local-partial-types
...

if cond():
    for x in range(n):
        # Type of 'x' is 'int' here
        ...
else:
    for x in ['a', 'b']:
        # Type of 'x' is 'str' here
        ...

We are planning to turn this flag on by default in mypy 2.0, along with --local-partial-types. The feature is still experimental and has known issues, and the semantics may still change in the future. You may need to update or add type annotations when switching to the new behavior, but if you encounter anything unexpected, please create a GitHub issue.

This was contributed by Jukka Lehtosalo (PR 18727, PR 19153).

Stricter Type Checking with Imprecise Types

Mypy can now detect additional errors in code that uses Any types or has missing function annotations.

When calling dict.get(x, None) on an object of type dict[str, Any], this now results in an optional type (in the past it was Any):

def f(d: dict[str, Any]) -> int:
    # Error: Return value is "Any | None" but expected "int"
    return d.get("x", None)

Type narrowing using assignments can result in more precise types in the presence of Any types:

def foo(): ...

def bar(n: int) -> None:
    x = foo()
    # Type of 'x' is 'Any' here
    if n > 5:
        x = str(n)
        # Type of 'x' is 'str' here

When using --check-untyped-defs, unannotated overrides are now checked more strictly against superclass definitions.

Related PRs:

Improvements to Attribute Resolution

This release includes several fixes to inconsistent resolution of attribute, method and descriptor types.

Make Implementation for Abstract Overloads Optional

The implementation can now be omitted for abstract overloaded methods, even outside stubs:

from abc import abstractmethod
from typing import overload

class C:
    @abstractmethod
    @overload
    def foo(self, x: int) -> int: ...

    @abstractmethod
    @overload
    def foo(self, x: str) -> str: ...

    # No implementation required for "foo"

This was contributed by Ivan Levkivskyi (PR 18882).

Option to Exclude Everything in .gitignore

You can now use --exclude-gitignore to exclude everything in a.gitignore file from the mypy build. This behaves similar to excluding the paths using --exclude. We might enable this by default in a future mypy release.

This was contributed by Ivan Levkivskyi (PR 18696).

Selectively Disable Deprecated Warnings

It's now possible to selectively disable warnings generated fromwarnings.deprecatedusing the --deprecated-calls-excludeoption:

# mypy --enable-error-code deprecated
#      --deprecated-calls-exclude=foo.A
import foo

foo.A().func()  # OK, the deprecated warning is ignored
# file foo.py

from typing_extensions import deprecated

class A:
    @deprecated("Use A.func2 instead")
    def func(self): pass

    ...

Contributed by Marc Mueller (PR 18641)

Annotating Native/Non-Native Classes in Mypyc

You can now declare a class as a non-native class when compiling with mypyc. Unlike native classes, which are extension classes and have an immutable structure, non-native classes are normal Python classes at runtime and are fully dynamic. Example:

from mypy_extensions import mypyc_attr

@mypyc_attr(native_class=False)
class NonNativeClass:
    ...

o = NonNativeClass()

# Ok, even if attribute "foo" not declared in class body
setattr(o, "foo", 1)

Classes are native by default in compiled modules, but classes that use certain features (such as most metaclasses) are implicitly non-native.

You can also explicitly declare a class as native. In this case mypyc will generate an error if it can't compile the class as a native class, instead of falling back to a non-native class:

from mypy_extensions import mypyc_attr
from foo import MyMeta

# Error: Unsupported metaclass for a native class
@mypyc_attr(native_class=True)
class C(metaclass=MyMeta):
    ...

Since native classes are significantly more efficient that non-native classes, you may want to ensure that certain classes always compiled as native classes.

This feature was contributed by Valentin Stanciu (PR 18802).

Mypyc Fixes and Improvements

Fixes to Crashes

Performance Improvements

These are specific to mypy. Mypyc-related performance improvements are discussed elsewhere.

Documentation Updates

Stubgen Improvements

Stubtest Improvements

Miscellaneous 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.