Thursday 6 April 2023

Mypy 1.2 Released

We’ve just uploaded mypy 1.2 to the Python Package Index (PyPI). Mypy is a static type checker for Python. This release includes new features, performance improvements 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.

Improvements to Dataclass Transforms

  • Support implicit default for "init" parameter in field specifiers (Wesley Collin Wright and Jukka Lehtosalo, PR 15010)
  • Support descriptors in dataclass transform (Jukka Lehtosalo, PR 15006)
  • Fix frozen_default in incremental mode (Wesley Collin Wright)
  • Fix frozen behavior for base classes with direct metaclasses (Wesley Collin Wright, PR 14878)

Mypyc: Native Floats

Mypyc now uses a native, unboxed representation for values of type float. Previously these were heap-allocated Python objects. Native floats are faster and use less memory. Code that uses floating-point operations heavily can be several times faster when using native floats.

Various float operations and math functions also now have optimized implementations. Refer to the documentation for a full list.

This can change the behavior of existing code that uses subclasses of float. When assigning an instance of a subclass of float to a variable with the float type, it gets implicitly converted to a float instance when compiled:

    from lib import MyFloat  # MyFloat ia a subclass of "float"
    def example() -> None:
        x = MyFloat(1.5)
        y: float = x  # Implicit conversion from MyFloat to float
        print(type(y))  # float, not MyFloat

Previously, implicit conversions were applied to int subclasses but not float subclasses.

Also, int values can no longer be assigned to a variable with type float in compiled code, since these types now have incompatible representations. An explicit conversion is required:

    def example(n: int) -> None:
        a: float = 1  # Error: cannot assign "int" to "float"
        b: float = 1.0  # OK
        c: float = n  # Error
        d: float = float(n)  # OK

This restriction only applies to assignments, since they could otherwise narrow down the type of a variable from float to int. int values can still be implicitly converted to float when passed as arguments to functions that expect float values.

Note that mypyc still doesn’t support arrays of unboxed float values. Using list[float] involves heap-allocated float objects, since list can only store boxed values. Support for efficient floating point arrays is one of the next major planned mypyc features.

Related changes:

  • Use a native unboxed representation for floats (Jukka Lehtosalo, PR 14880)
  • Document native floats and integers (Jukka Lehtosalo, PR 14927)
  • Fixes to float to int conversion (Jukka Lehtosalo, PR 14936)

Mypyc: Native Integers

Mypyc now supports signed 32-bit and 64-bit integer types in addition to the arbitrary-precision int type. You can use the types mypy_extensions.i32 and mypy_extensions.i64 to speed up code that uses integer operations heavily.

Simple example:

    from mypy_extensions import i64
    def inc(x: i64) -> i64:
        return x + 1

Refer to the documentation for more information. This feature was contributed by Jukka Lehtosalo.

Other Mypyc Fixes and Improvements

  • Support iterating over a TypedDict (Richard Si, PR 14747)
  • Faster coercions between different tuple types (Jukka Lehtosalo, PR 14899)
  • Faster calls via type aliases (Jukka Lehtosalo, PR 14784)
  • Faster classmethod calls via cls (Jukka Lehtosalo, PR 14789)

Fixes to Crashes

  • Fix crash on class-level import in protocol definition (Ivan Levkivskyi, PR 14926)
  • Fix crash on single item union of alias (Ivan Levkivskyi, PR 14876)
  • Fix crash on ParamSpec in incremental mode (Ivan Levkivskyi, PR 14885)

Documentation Updates

  • Update adopting --strict documentation for 1.0 (Shantanu, PR 14865)
  • Some minor documentation tweaks (Jukka Lehtosalo, PR 14847)
  • Improve documentation of top level mypy: disable-error-code comment (Nikita Sobolev, PR 14810)

Error Reporting Improvements

  • Add error code to typing_extensions suggestion (Shantanu, PR 14881)
  • Add a separate error code for top-level await (Nikita Sobolev, PR 14801)
  • Don’t suggest two obsolete stub packages (Jelle Zijlstra, PR 14842)
  • Add suggestions for pandas-stubs and lxml-stubs (Shantanu, PR 14737)

Other Fixes and Improvements

  • Multiple inheritance considers callable objects as subtypes of functions (Christoph Tyralla, PR 14855)
  • stubtest: Respect @final runtime decorator and enforce it in stubs (Nikita Sobolev, PR 14922)
  • Fix false positives related to type[<type-var>] (sterliakov, PR 14756)
  • Fix duplication of ParamSpec prefixes and properly substitute ParamSpecs (EXPLOSION, PR 14677)
  • Fix line number if __iter__ is incorrectly reported as missing (Jukka Lehtosalo, PR 14893)
  • Fix incompatible overrides of overloaded generics with self types (Shantanu, PR 14882)
  • Allow SupportsIndex in slice expressions (Shantanu, PR 14738)
  • Support if statements in bodies of dataclasses and classes that use dataclass_transform (Jacek Chałupka, PR 14854)
  • Allow iterable class objects to be unpacked (including enums) (Alex Waygood, PR 14827)
  • Fix narrowing for walrus expressions used in match statements (Shantanu, PR 14844)
  • Add signature for attr.evolve (Ilya Konstantinov, PR 14526)
  • Fix Any inference when unpacking iterators that don't directly inherit from typing.Iterator (Alex Waygood, PR 14821)
  • Fix unpack with overloaded __iter__ method (Nikita Sobolev, PR 14817)
  • Reduce size of JSON data in mypy cache (dosisod, PR 14808)
  • Improve “used before definition” checks when a local definition has the same name as a global definition (Stas Ilinskiy, PR 14517)
  • Honor NoReturn as __setitem__ return type to mark unreachable code (sterliakov, PR 12572)

Typeshed Updates

Typeshed is now modular and distributed as separate PyPI packages for everything except the standard library stubs. Please see git log for full list of typeshed changes.

Acknowledgements

Thanks to all mypy contributors who contributed to this release:

  • Alex Waygood
  • Avasam
  • Christoph Tyralla
  • dosisod
  • EXPLOSION
  • Ilya Konstantinov
  • Ivan Levkivskyi
  • Jacek Chałupka
  • Jelle Zijlstra
  • Jukka Lehtosalo
  • Marc Mueller
  • Max Murin
  • Nikita Sobolev
  • Richard Si
  • Shantanu
  • Stas Ilinskiy
  • sterliakov
  • Wesley Collin Wright