Skip to content

Commit

Permalink
Merge branch 'master' into pythongh-16649-type-comments-and-constrain…
Browse files Browse the repository at this point in the history
…ed-generics
  • Loading branch information
picnixz authored Mar 17, 2024
2 parents 9d6f56f + 00220bd commit 5ebb7b9
Show file tree
Hide file tree
Showing 296 changed files with 8,671 additions and 4,027 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build_wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
- uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Trigger script
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
VERIFY_MYPY_ERROR_CODES: 1
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
- uses: actions/setup-python@v5
with:
python-version: '3.8'
- name: Install tox
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/mypy_primer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
with:
path: mypy_to_test
fetch-depth: 0
- uses: actions/setup-python@v4
- uses: actions/setup-python@v5
with:
python-version: "3.10"
- name: Install dependencies
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/mypy_primer_comment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
- name: Post comment
id: post-comment
uses: actions/github-script@v6
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/sync_typeshed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
fetch-depth: 0
# TODO: use whatever solution ends up working for
# https://github.com/python/typeshed/issues/8434
- uses: actions/setup-python@v4
- uses: actions/setup-python@v5
with:
python-version: "3.10"
- name: git config
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ jobs:
PYTEST_ADDOPTS: --color=yes
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python }}
architecture: ${{ matrix.arch }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test_stubgenc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- uses: actions/checkout@v4

- name: Setup 🐍 3.8
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: 3.8

Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repos:
- id: black
exclude: '^(test-data/)'
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.15 # must match test-requirements.txt
rev: v0.2.0 # must match test-requirements.txt
hooks:
- id: ruff
args: [--exit-non-zero-on-fix]
Expand Down
137 changes: 136 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,141 @@
# Mypy Release Notes

## Next release
## Mypy 1.9

We’ve just uploaded mypy 1.9 to the Python Package Index ([PyPI](https://pypi.org/project/mypy/)). 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](http://mypy.readthedocs.io).

#### Breaking Changes

Because the version of typeshed we use in mypy 1.9 doesn't support 3.7, neither does mypy 1.9. (Jared Hance, PR [16883](https://github.com/python/mypy/pull/16883))

We are planning to enable
[local partial types](https://mypy.readthedocs.io/en/stable/command_line.html#cmdoption-mypy-local-partial-types) (enabled via the
`--local-partial-types` flag) later this year by default. This change
was announced years ago, but now it's finally happening. This is a
major backward-incompatible change, so we'll probably include it as
part of the upcoming mypy 2.0 release. This makes daemon and
non-daemon mypy runs have the same behavior by default.

Local partial types can also be enabled in the mypy config file:
```
local_partial_types = True
```

We are looking at providing a tool to make it easier to migrate
projects to use `--local-partial-types`, but it's not yet clear whether
this is practical. The migration usually involves adding some
explicit type annotations to module-level and class-level variables.

#### Basic Support for Type Parameter Defaults (PEP 696)

This release contains new experimental support for type parameter
defaults ([PEP 696](https://peps.python.org/pep-0696)). Please try it
out! This feature was contributed by Marc Mueller.

Since this feature will be officially introduced in the next Python
feature release (3.13), you will need to import `TypeVar`, `ParamSpec`
or `TypeVarTuple` from `typing_extensions` to use defaults for now.

This example adapted from the PEP defines a default for `BotT`:
```python
from typing import Generic
from typing_extensions import TypeVar

class Bot: ...

BotT = TypeVar("BotT", bound=Bot, default=Bot)

class Context(Generic[BotT]):
bot: BotT

class MyBot(Bot): ...

# type is Bot (the default)
reveal_type(Context().bot)
# type is MyBot
reveal_type(Context[MyBot]().bot)
```

#### Type-checking Improvements
* Fix missing type store for overloads (Marc Mueller, PR [16803](https://github.com/python/mypy/pull/16803))
* Fix `'WriteToConn' object has no attribute 'flush'` (Charlie Denton, PR [16801](https://github.com/python/mypy/pull/16801))
* Improve TypeAlias error messages (Marc Mueller, PR [16831](https://github.com/python/mypy/pull/16831))
* Support narrowing unions that include `type[None]` (Christoph Tyralla, PR [16315](https://github.com/python/mypy/pull/16315))
* Support TypedDict functional syntax as class base type (anniel-stripe, PR [16703](https://github.com/python/mypy/pull/16703))
* Accept multiline quoted annotations (Shantanu, PR [16765](https://github.com/python/mypy/pull/16765))
* Allow unary + in `Literal` (Jelle Zijlstra, PR [16729](https://github.com/python/mypy/pull/16729))
* Substitute type variables in return type of static methods (Kouroche Bouchiat, PR [16670](https://github.com/python/mypy/pull/16670))
* Consider TypeVarTuple to be invariant (Marc Mueller, PR [16759](https://github.com/python/mypy/pull/16759))
* Add `alias` support to `field()` in `attrs` plugin (Nikita Sobolev, PR [16610](https://github.com/python/mypy/pull/16610))
* Improve attrs hashability detection (Tin Tvrtković, PR [16556](https://github.com/python/mypy/pull/16556))

#### Performance Improvements

* Speed up finding function type variables (Jukka Lehtosalo, PR [16562](https://github.com/python/mypy/pull/16562))

#### Documentation Updates

* Document supported values for `--enable-incomplete-feature` in "mypy --help" (Froger David, PR [16661](https://github.com/python/mypy/pull/16661))
* Update new type system discussion links (thomaswhaley, PR [16841](https://github.com/python/mypy/pull/16841))
* Add missing class instantiation to cheat sheet (Aleksi Tarvainen, PR [16817](https://github.com/python/mypy/pull/16817))
* Document how evil `--no-strict-optional` is (Shantanu, PR [16731](https://github.com/python/mypy/pull/16731))
* Improve mypy daemon documentation note about local partial types (Makonnen Makonnen, PR [16782](https://github.com/python/mypy/pull/16782))
* Fix numbering error (Stefanie Molin, PR [16838](https://github.com/python/mypy/pull/16838))
* Various documentation improvements (Shantanu, PR [16836](https://github.com/python/mypy/pull/16836))

#### Stubtest Improvements
* Ignore private function/method parameters when they are missing from the stub (private parameter names start with a single underscore and have a default) (PR [16507](https://github.com/python/mypy/pull/16507))
* Ignore a new protocol dunder (Alex Waygood, PR [16895](https://github.com/python/mypy/pull/16895))
* Private parameters can be omitted (Sebastian Rittau, PR [16507](https://github.com/python/mypy/pull/16507))
* Add support for setting enum members to "..." (Jelle Zijlstra, PR [16807](https://github.com/python/mypy/pull/16807))
* Adjust symbol table logic (Shantanu, PR [16823](https://github.com/python/mypy/pull/16823))
* Fix posisitional-only handling in overload resolution (Shantanu, PR [16750](https://github.com/python/mypy/pull/16750))

#### Stubgen Improvements
* Fix crash on star unpack of TypeVarTuple (Ali Hamdan, PR [16869](https://github.com/python/mypy/pull/16869))
* Use PEP 604 unions everywhere (Ali Hamdan, PR [16519](https://github.com/python/mypy/pull/16519))
* Do not ignore property deleter (Ali Hamdan, PR [16781](https://github.com/python/mypy/pull/16781))
* Support type stub generation for `staticmethod` (WeilerMarcel, PR [14934](https://github.com/python/mypy/pull/14934))

#### Acknowledgements

​Thanks to all mypy contributors who contributed to this release:

- Aleksi Tarvainen
- Alex Waygood
- Ali Hamdan
- anniel-stripe
- Charlie Denton
- Christoph Tyralla
- Dheeraj
- Fabian Keller
- Fabian Lewis
- Froger David
- Ihor
- Jared Hance
- Jelle Zijlstra
- Jukka Lehtosalo
- Kouroche Bouchiat
- Lukas Geiger
- Maarten Huijsmans
- Makonnen Makonnen
- Marc Mueller
- Nikita Sobolev
- Sebastian Rittau
- Shantanu
- Stefanie Molin
- Stephen Morton
- thomaswhaley
- Tin Tvrtković
- WeilerMarcel
- Wesley Collin Wright
- zipperer

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

## Mypy 1.8

Expand Down
2 changes: 2 additions & 0 deletions docs/source/cheat_sheet_py3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ Classes

.. code-block:: python
from typing import ClassVar
class BankAccount:
# The "__init__" method doesn't return anything, so it gets return
# type "None" just like any other method that doesn't return anything
Expand Down
4 changes: 2 additions & 2 deletions docs/source/common_issues.rst
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ Consider this example:

.. code-block:: python
from typing_extensions import Protocol
from typing import Protocol
class P(Protocol):
x: float
Expand All @@ -561,7 +561,7 @@ the protocol definition:

.. code-block:: python
from typing_extensions import Protocol
from typing import Protocol
class P(Protocol):
@property
Expand Down
8 changes: 4 additions & 4 deletions docs/source/error_code_list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ Example:

.. code-block:: python
from typing_extensions import TypedDict
from typing import TypedDict
class Point(TypedDict):
x: int
Expand All @@ -562,7 +562,7 @@ to have been validated at the point of construction. Example:

.. code-block:: python
from typing_extensions import TypedDict
from typing import TypedDict
class Point(TypedDict):
x: int
Expand Down Expand Up @@ -868,7 +868,7 @@ the return type affects which lines mypy thinks are reachable after a
``True`` may swallow exceptions. An imprecise return type can result
in mysterious errors reported near ``with`` statements.

To fix this, use either ``typing_extensions.Literal[False]`` or
To fix this, use either ``typing.Literal[False]`` or
``None`` as the return type. Returning ``None`` is equivalent to
returning ``False`` in this context, since both are treated as false
values.
Expand Down Expand Up @@ -897,7 +897,7 @@ You can use ``Literal[False]`` to fix the error:

.. code-block:: python
from typing_extensions import Literal
from typing import Literal
class MyContext:
...
Expand Down
24 changes: 20 additions & 4 deletions docs/source/error_code_list2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -484,11 +484,11 @@ Example:
.. _code-mutable-override:

Check that overrides of mutable attributes are safe
---------------------------------------------------
Check that overrides of mutable attributes are safe [mutable-override]
----------------------------------------------------------------------

This will enable the check for unsafe overrides of mutable attributes. For
historical reasons, and because this is a relatively common pattern in Python,
`mutable-override` will enable the check for unsafe overrides of mutable attributes.
For historical reasons, and because this is a relatively common pattern in Python,
this check is not enabled by default. The example below is unsafe, and will be
flagged when this error code is enabled:

Expand Down Expand Up @@ -555,3 +555,19 @@ Correct usage:
When this code is enabled, using ``reveal_locals`` is always an error,
because there's no way one can import it.

.. _code-narrowed-type-not-subtype:

Check that ``TypeIs`` narrows types [narrowed-type-not-subtype]
---------------------------------------------------------------

:pep:`742` requires that when ``TypeIs`` is used, the narrowed
type must be a subtype of the original type::

from typing_extensions import TypeIs

def f(x: int) -> TypeIs[str]: # Error, str is not a subtype of int
...

def g(x: object) -> TypeIs[str]: # OK
...
3 changes: 1 addition & 2 deletions docs/source/generics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -770,8 +770,7 @@ protocols mostly follow the normal rules for generic classes. Example:

.. code-block:: python
from typing import TypeVar
from typing_extensions import Protocol
from typing import Protocol, TypeVar
T = TypeVar('T')
Expand Down
16 changes: 6 additions & 10 deletions docs/source/protocols.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ compatible as types: nominal subtyping and structural subtyping.
*Nominal* subtyping is strictly based on the class hierarchy. If class ``Dog``
inherits class ``Animal``, it's a subtype of ``Animal``. Instances of ``Dog``
can be used when ``Animal`` instances are expected. This form of subtyping
subtyping is what Python's type system predominantly uses: it's easy to
is what Python's type system predominantly uses: it's easy to
understand and produces clear and concise error messages, and matches how the
native :py:func:`isinstance <isinstance>` check works -- based on class
hierarchy.
Expand Down Expand Up @@ -68,8 +68,7 @@ class:

.. code-block:: python
from typing import Iterable
from typing_extensions import Protocol
from typing import Iterable, Protocol
class SupportsClose(Protocol):
# Empty method body (explicit '...')
Expand Down Expand Up @@ -226,8 +225,7 @@ such as trees and linked lists:

.. code-block:: python
from typing import TypeVar, Optional
from typing_extensions import Protocol
from typing import TypeVar, Optional, Protocol
class TreeLike(Protocol):
value: int
Expand Down Expand Up @@ -255,7 +253,7 @@ rudimentary support for runtime structural checks:

.. code-block:: python
from typing_extensions import Protocol, runtime_checkable
from typing import Protocol, runtime_checkable
@runtime_checkable
class Portable(Protocol):
Expand Down Expand Up @@ -298,8 +296,7 @@ member:

.. code-block:: python
from typing import Optional, Iterable
from typing_extensions import Protocol
from typing import Optional, Iterable, Protocol
class Combiner(Protocol):
def __call__(self, *vals: bytes, maxlen: Optional[int] = None) -> list[bytes]: ...
Expand All @@ -323,8 +320,7 @@ a double underscore prefix is used. For example:

.. code-block:: python
from typing import Callable, TypeVar
from typing_extensions import Protocol
from typing import Callable, Protocol, TypeVar
T = TypeVar('T')
Expand Down
2 changes: 1 addition & 1 deletion docs/source/stubs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ For example:

.. code-block:: python
from typing_extensions import Protocol
from typing import Protocol
class Resource(Protocol):
def ok_1(self, foo: list[str] = ...) -> None: ...
Expand Down
Loading

0 comments on commit 5ebb7b9

Please sign in to comment.