Skip to content

Commit 01b8a81

Browse files
Merge branch 'master' into bug/generic-namedtuple-match-pattern
2 parents e7d0b25 + 232697e commit 01b8a81

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+7830
-4468
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ repos:
2626
hooks:
2727
- id: codespell
2828
args:
29-
- --ignore-words-list=HAX,ccompiler,ot,statics,whet,zar
29+
- --ignore-words-list=HAX,Nam,ccompiler,ot,statics,whet,zar
3030
exclude: ^(mypy/test/|mypy/typeshed/|mypyc/test-data/|test-data/).+$
3131
- repo: https://github.com/rhysd/actionlint
3232
rev: v1.7.7

CHANGELOG.md

Lines changed: 4771 additions & 4291 deletions
Large diffs are not rendered by default.

MANIFEST.in

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
prune mypy/typeshed
66
include mypy/typeshed/LICENSE
77
include mypy/typeshed/stdlib/VERSIONS
8+
include mypy/typeshed/stdlib/_typeshed/README.md
89
recursive-include mypy/typeshed *.pyi
910

1011
# mypy and mypyc
@@ -38,14 +39,16 @@ include test-requirements.in
3839
include test-requirements.txt
3940
include mypy_self_check.ini
4041
prune misc
42+
include misc/diff-cache.py
43+
include misc/apply-cache-diff.py
4144
graft test-data
4245
graft mypy/test
4346
include conftest.py
4447
include runtests.py
4548
include tox.ini
4649

47-
include LICENSE mypyc/README.md CHANGELOG.md
48-
exclude .gitmodules CONTRIBUTING.md CREDITS ROADMAP.md action.yml .editorconfig
50+
include LICENSE mypyc/README.md CHANGELOG.md CREDITS
51+
exclude .gitmodules CONTRIBUTING.md ROADMAP.md action.yml .editorconfig
4952
exclude .git-blame-ignore-revs .pre-commit-config.yaml
5053

5154
global-exclude *.py[cod]

docs/source/command_line.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ potentially problematic or redundant in some way.
551551
.. note::
552552

553553
Mypy currently cannot detect and report unreachable or redundant code
554-
inside any functions using :ref:`type-variable-value-restriction`.
554+
inside any functions using :ref:`value-constrained type variables <value-constrained-type-variables>`.
555555

556556
This limitation will be removed in future releases of mypy.
557557

@@ -598,7 +598,7 @@ of the above sections.
598598
.. option:: --allow-redefinition-new
599599

600600
By default, mypy won't allow a variable to be redefined with an
601-
unrelated type. This flag enables the redefinition of unannotated
601+
unrelated type. This flag enables the redefinition of *unannotated*
602602
variables with an arbitrary type. You will also need to enable
603603
:option:`--local-partial-types <mypy --local-partial-types>`.
604604
Example:
@@ -645,7 +645,6 @@ of the above sections.
645645
646646
Note: We are planning to turn this flag on by default in a future mypy
647647
release, along with :option:`--local-partial-types <mypy --local-partial-types>`.
648-
The feature is still experimental, and the semantics may still change.
649648

650649
.. option:: --allow-redefinition
651650

docs/source/common_issues.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,13 @@ See :ref:`type-narrowing` for more information.
303303
Invariance vs covariance
304304
------------------------
305305

306-
Most mutable generic collections are invariant, and mypy considers all
307-
user-defined generic classes invariant by default
308-
(see :ref:`variance-of-generics` for motivation). This could lead to some
306+
Most mutable generic collections are invariant. When using the legacy
307+
``TypeVar`` syntax, mypy considers all user-defined generic classes invariant
308+
by default (see :ref:`variance-of-generics` for motivation). When using the
309+
:pep:`695` syntax (``class MyClass[T]: ...``), variance is inferred from
310+
usage rather than defaulting to invariant.
311+
312+
The fact that mutable sequences are usually invariant can lead to some
309313
unexpected errors when combined with type inference. For example:
310314

311315
.. code-block:: python

docs/source/generics.rst

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -693,10 +693,11 @@ contravariant, use type variables defined with special keyword arguments
693693
my_box = Box(Square())
694694
look_into(my_box) # OK, but mypy would complain here for an invariant type
695695
696+
.. _value-constrained-type-variables:
696697
.. _type-variable-value-restriction:
697698

698-
Type variables with value restriction
699-
*************************************
699+
Value-constrained type variables
700+
*********************************
700701

701702
By default, a type variable can be replaced with any type -- or any type that
702703
is a subtype of the upper bound, which defaults to ``object``. However, sometimes
@@ -727,8 +728,9 @@ The same thing is also possibly using the legacy syntax (Python 3.11 or earlier)
727728
def concat(x: AnyStr, y: AnyStr) -> AnyStr:
728729
return x + y
729730
730-
No matter which syntax you use, such a type variable is called a type variable
731-
with a value restriction. Importantly, this is different from a union type,
731+
No matter which syntax you use, such a type variable is called a
732+
value-constrained type variable (the allowed types are accessible at runtime
733+
via ``TypeVar.__constraints__``). Importantly, this is different from a union type,
732734
since combinations of ``str`` and ``bytes`` are not accepted:
733735

734736
.. code-block:: python
@@ -760,7 +762,7 @@ for the type variable, which in this case is ``str``.
760762

761763
This is thus subtly different from using ``str | bytes`` as an upper bound,
762764
where the return type would be ``S`` (see :ref:`type-variable-upper-bound`).
763-
Using a value restriction is correct for ``concat``, since ``concat``
765+
Using a value constraint is correct for ``concat``, since ``concat``
764766
actually returns a ``str`` instance in the above example:
765767

766768
.. code-block:: python
@@ -775,7 +777,7 @@ value of :py:func:`re.compile`, where ``S`` can be either ``str``
775777
or ``bytes``. Regular expressions can be based on a string or a
776778
bytes pattern.
777779

778-
A type variable may not have both a value restriction and an upper bound.
780+
A type variable may not have both value constraints and an upper bound.
779781

780782
Note that you may come across :py:data:`~typing.AnyStr` imported from
781783
:py:mod:`typing`. This feature is now deprecated, but it means the same
@@ -1330,7 +1332,7 @@ Here are examples using the legacy syntax (Python 3.11 and earlier):
13301332
for i, j in NewVec[int]():
13311333
...
13321334
1333-
Using type variable bounds or value restriction in generic aliases has
1335+
Using type variable bounds or value constraints in generic aliases has
13341336
the same effect as in generic classes and functions.
13351337

13361338

docs/source/more_types.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,8 @@ Here is the same example using the legacy syntax (Python 3.11 and earlier):
303303
.. note::
304304

305305
If you just need to constrain a type variable to certain types or
306-
subtypes, you can use a :ref:`value restriction
307-
<type-variable-value-restriction>`.
306+
subtypes, you can use a :ref:`value-constrained type variable
307+
<value-constrained-type-variables>`.
308308

309309
The default values of a function's arguments don't affect its signature -- only
310310
the absence or presence of a default value does. So in order to reduce

docs/source/stubtest.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ The rest of this section documents the command line interface of stubtest.
183183

184184
Ignore errors for whether an argument should or shouldn't be positional-only
185185

186+
.. option:: --strict-type-check-only
187+
188+
Require :py:func:`@type_check_only <typing.type_check_only>` on private types
189+
that are not present at runtime.
190+
186191
.. option:: --allowlist FILE
187192

188193
Use file as an allowlist. Can be passed multiple times to combine multiple

mypy/build.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@
146146
from mypy.fixup import fixup_module
147147
from mypy.freetree import free_tree
148148
from mypy.fscache import FileSystemCache
149+
from mypy.known_modules import get_known_modules, reset_known_modules_cache
150+
from mypy.messages import best_matches, pretty_seq
149151
from mypy.metastore import FilesystemMetadataStore, MetadataStore, SqliteMetadataStore
150152
from mypy.modulefinder import (
151153
BuildSource as BuildSource,
@@ -355,6 +357,7 @@ def build(
355357

356358
# This is mostly for the benefit of tests that use builtins fixtures.
357359
instance_cache.reset()
360+
reset_known_modules_cache()
358361

359362
def default_flush_errors(
360363
filename: str | None, new_messages: list[str], is_serious: bool
@@ -3562,6 +3565,27 @@ def module_not_found(
35623565
code = codes.IMPORT
35633566
manager.error(line, msg.format(module=target), code=code)
35643567

3568+
if (
3569+
reason == ModuleNotFoundReason.NOT_FOUND
3570+
and not errors.prefer_simple_messages()
3571+
and line not in errors.ignored_lines.get(caller_state.xpath, {})
3572+
):
3573+
top_level_target = target.split(".")[0]
3574+
if not top_level_target.startswith("_"):
3575+
known_modules = get_known_modules(
3576+
manager.find_module_cache.stdlib_py_versions, manager.options.python_version
3577+
)
3578+
matches = best_matches(top_level_target, known_modules, n=3)
3579+
matches = [m for m in matches if m.lower() != top_level_target.lower()]
3580+
if matches:
3581+
errors.report(
3582+
line,
3583+
0,
3584+
f'Did you mean {pretty_seq(matches, "or")}?',
3585+
severity="note",
3586+
code=code,
3587+
)
3588+
35653589
dist = stub_distribution_name(target)
35663590
for note in notes:
35673591
if "{stub_dist}" in note:

mypy/checker.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6803,11 +6803,14 @@ def narrow_type_by_identity_equality(
68036803
# It is correct to always narrow here. It improves behaviour on tests and
68046804
# detects many inaccurate type annotations on primer.
68056805
# However, because mypy does not currently check unreachable code, it feels
6806-
# risky to narrow to unreachable without --warn-unreachable.
6806+
# risky to narrow to unreachable without --warn-unreachable or not
6807+
# at module level
68076808
# See also this specific primer comment, where I force primer to run with
68086809
# --warn-unreachable to see what code we would stop checking:
68096810
# https://github.com/python/mypy/pull/20660#issuecomment-3865794148
6810-
if self.options.warn_unreachable or not is_unreachable_map(if_map):
6811+
if (
6812+
self.options.warn_unreachable and len(self.scope.stack) != 1
6813+
) or not is_unreachable_map(if_map):
68116814
all_if_maps.append(if_map)
68126815

68136816
# Handle narrowing for operands with custom __eq__ methods specially

0 commit comments

Comments
 (0)