Skip to content

Commit 0fdd0bc

Browse files
authored
Merge branch 'master' into bugfix/pythongh-19093-infer-constraints-include-fallback
2 parents 8b23cf0 + 8c772c7 commit 0fdd0bc

File tree

277 files changed

+5060
-4781
lines changed

Some content is hidden

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

277 files changed

+5060
-4781
lines changed

CHANGELOG.md

Lines changed: 405 additions & 10 deletions
Large diffs are not rendered by default.

docs/source/command_line.rst

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,12 +593,58 @@ of the above sections.
593593
This flag causes mypy to suppress errors caused by not being able to fully
594594
infer the types of global and class variables.
595595

596-
.. option:: --allow-redefinition
596+
.. option:: --allow-redefinition-new
597597

598598
By default, mypy won't allow a variable to be redefined with an
599-
unrelated type. This flag enables redefinition of a variable with an
599+
unrelated type. This *experimental* flag enables the redefinition of
600+
unannotated variables with an arbitrary type. You will also need to enable
601+
:option:`--local-partial-types <mypy --local-partial-types>`.
602+
Example:
603+
604+
.. code-block:: python
605+
606+
def maybe_convert(n: int, b: bool) -> int | str:
607+
if b:
608+
x = str(n) # Assign "str"
609+
else:
610+
x = n # Assign "int"
611+
# Type of "x" is "int | str" here.
612+
return x
613+
614+
Without the new flag, mypy only supports inferring optional types
615+
(``X | None``) from multiple assignments. With this option enabled,
616+
mypy can infer arbitrary union types.
617+
618+
This also enables an unannotated variable to have different types in different
619+
code locations:
620+
621+
.. code-block:: python
622+
623+
if check():
624+
for x in range(n):
625+
# Type of "x" is "int" here.
626+
...
627+
else:
628+
for x in ['a', 'b']:
629+
# Type of "x" is "str" here.
630+
...
631+
632+
Note: We are planning to turn this flag on by default in a future mypy
633+
release, along with :option:`--local-partial-types <mypy --local-partial-types>`.
634+
The feature is still experimental, and the semantics may still change.
635+
636+
.. option:: --allow-redefinition
637+
638+
This is an older variant of
639+
:option:`--allow-redefinition-new <mypy --allow-redefinition-new>`.
640+
This flag enables redefinition of a variable with an
600641
arbitrary type *in some contexts*: only redefinitions within the
601642
same block and nesting depth as the original definition are allowed.
643+
644+
We have no plans to remove this flag, but we expect that
645+
:option:`--allow-redefinition-new <mypy --allow-redefinition-new>`
646+
will replace this flag for new use cases eventually.
647+
602648
Example where this can be useful:
603649

604650
.. code-block:: python

docs/source/config_file.rst

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,44 @@ section of the command line docs.
713713
Causes mypy to suppress errors caused by not being able to fully
714714
infer the types of global and class variables.
715715

716+
.. confval:: allow_redefinition_new
717+
718+
:type: boolean
719+
:default: False
720+
721+
By default, mypy won't allow a variable to be redefined with an
722+
unrelated type. This *experimental* flag enables the redefinition of
723+
unannotated variables with an arbitrary type. You will also need to enable
724+
:confval:`local_partial_types`.
725+
Example:
726+
727+
.. code-block:: python
728+
729+
def maybe_convert(n: int, b: bool) -> int | str:
730+
if b:
731+
x = str(n) # Assign "str"
732+
else:
733+
x = n # Assign "int"
734+
# Type of "x" is "int | str" here.
735+
return x
736+
737+
This also enables an unannotated variable to have different types in different
738+
code locations:
739+
740+
.. code-block:: python
741+
742+
if check():
743+
for x in range(n):
744+
# Type of "x" is "int" here.
745+
...
746+
else:
747+
for x in ['a', 'b']:
748+
# Type of "x" is "str" here.
749+
...
750+
751+
Note: We are planning to turn this flag on by default in a future mypy
752+
release, along with :confval:`local_partial_types`.
753+
716754
.. confval:: allow_redefinition
717755

718756
:type: boolean
@@ -746,6 +784,7 @@ section of the command line docs.
746784

747785
Disallows inferring variable type for ``None`` from two assignments in different scopes.
748786
This is always implicitly enabled when using the :ref:`mypy daemon <mypy_daemon>`.
787+
This will be enabled by default in a future mypy release.
749788

750789
.. confval:: disable_error_code
751790

docs/source/generics.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ Using ``Stack`` is similar to built-in container types:
9393
stack.push('x')
9494
9595
stack2: Stack[str] = Stack()
96-
stack2.append('x')
96+
stack2.push('x')
9797
9898
Construction of instances of generic types is type checked (Python 3.12 syntax):
9999

misc/typeshed_patches/0001-Partially-revert-Clean-up-argparse-hacks.patch

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
From b5f2cc9633f9f6cd9326eee96a32efb3aff70701 Mon Sep 17 00:00:00 2001
1+
From 05f351f6a37fe8b73c698c348bf6aa5108363049 Mon Sep 17 00:00:00 2001
22
From: Marc Mueller <[email protected]>
33
Date: Sat, 15 Feb 2025 20:11:06 +0100
44
Subject: [PATCH] Partially revert Clean up argparse hacks
@@ -8,7 +8,7 @@ Subject: [PATCH] Partially revert Clean up argparse hacks
88
1 file changed, 5 insertions(+), 3 deletions(-)
99

1010
diff --git a/mypy/typeshed/stdlib/argparse.pyi b/mypy/typeshed/stdlib/argparse.pyi
11-
index 029bfeefe..9dbd8c308 100644
11+
index 95ad6c7da..79e6cfde1 100644
1212
--- a/mypy/typeshed/stdlib/argparse.pyi
1313
+++ b/mypy/typeshed/stdlib/argparse.pyi
1414
@@ -2,7 +2,7 @@ import sys
@@ -20,7 +20,7 @@ index 029bfeefe..9dbd8c308 100644
2020
from typing_extensions import Self, TypeAlias, deprecated
2121

2222
__all__ = [
23-
@@ -38,7 +38,9 @@ ONE_OR_MORE: Final = "+"
23+
@@ -36,7 +36,9 @@ ONE_OR_MORE: Final = "+"
2424
OPTIONAL: Final = "?"
2525
PARSER: Final = "A..."
2626
REMAINDER: Final = "..."
@@ -31,7 +31,7 @@ index 029bfeefe..9dbd8c308 100644
3131
ZERO_OR_MORE: Final = "*"
3232
_UNRECOGNIZED_ARGS_ATTR: Final = "_unrecognized_args" # undocumented
3333

34-
@@ -81,7 +83,7 @@ class _ActionsContainer:
34+
@@ -79,7 +81,7 @@ class _ActionsContainer:
3535
# more precisely, Literal["?", "*", "+", "...", "A...", "==SUPPRESS=="],
3636
# but using this would make it hard to annotate callers that don't use a
3737
# literal argument and for subclasses to override this method.
@@ -41,5 +41,5 @@ index 029bfeefe..9dbd8c308 100644
4141
default: Any = ...,
4242
type: _ActionType = ...,
4343
--
44-
2.48.1
44+
2.49.0
4545

misc/typeshed_patches/0001-Remove-use-of-LiteralString-in-builtins-13743.patch

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
From b4259edd94188f9e4cc77a22e768eea183a32053 Mon Sep 17 00:00:00 2001
1+
From e6995c91231e1915eba43a29a22dd4cbfaf9e08e Mon Sep 17 00:00:00 2001
22
From: Shantanu <[email protected]>
33
Date: Mon, 26 Sep 2022 12:55:07 -0700
44
Subject: [PATCH] Remove use of LiteralString in builtins (#13743)
@@ -8,18 +8,18 @@ Subject: [PATCH] Remove use of LiteralString in builtins (#13743)
88
1 file changed, 1 insertion(+), 99 deletions(-)
99

1010
diff --git a/mypy/typeshed/stdlib/builtins.pyi b/mypy/typeshed/stdlib/builtins.pyi
11-
index 63c53a5f6..d55042b56 100644
11+
index 00728f42d..ea77a730f 100644
1212
--- a/mypy/typeshed/stdlib/builtins.pyi
1313
+++ b/mypy/typeshed/stdlib/builtins.pyi
14-
@@ -63,7 +63,6 @@ from typing import ( # noqa: Y022
14+
@@ -63,7 +63,6 @@ from typing import ( # noqa: Y022,UP035
1515
from typing_extensions import ( # noqa: Y023
1616
Concatenate,
1717
Literal,
1818
- LiteralString,
1919
ParamSpec,
2020
Self,
2121
TypeAlias,
22-
@@ -438,31 +437,16 @@ class str(Sequence[str]):
22+
@@ -453,31 +452,16 @@ class str(Sequence[str]):
2323
def __new__(cls, object: object = ...) -> Self: ...
2424
@overload
2525
def __new__(cls, object: ReadableBuffer, encoding: str = ..., errors: str = ...) -> Self: ...
@@ -51,7 +51,7 @@ index 63c53a5f6..d55042b56 100644
5151
def format(self, *args: object, **kwargs: object) -> str: ...
5252
def format_map(self, mapping: _FormatMapMapping, /) -> str: ...
5353
def index(self, sub: str, start: SupportsIndex | None = ..., end: SupportsIndex | None = ..., /) -> int: ...
54-
@@ -478,99 +462,35 @@ class str(Sequence[str]):
54+
@@ -493,98 +477,34 @@ class str(Sequence[str]):
5555
def isspace(self) -> bool: ...
5656
def istitle(self) -> bool: ...
5757
def isupper(self) -> bool: ...
@@ -89,16 +89,15 @@ index 63c53a5f6..d55042b56 100644
8989
- ) -> LiteralString: ...
9090
- @overload
9191
def replace(self, old: str, new: str, count: SupportsIndex = -1, /) -> str: ... # type: ignore[misc]
92-
if sys.version_info >= (3, 9):
93-
- @overload
94-
- def removeprefix(self: LiteralString, prefix: LiteralString, /) -> LiteralString: ...
95-
- @overload
96-
def removeprefix(self, prefix: str, /) -> str: ... # type: ignore[misc]
97-
- @overload
98-
- def removesuffix(self: LiteralString, suffix: LiteralString, /) -> LiteralString: ...
99-
- @overload
100-
def removesuffix(self, suffix: str, /) -> str: ... # type: ignore[misc]
10192

93+
- @overload
94+
- def removeprefix(self: LiteralString, prefix: LiteralString, /) -> LiteralString: ...
95+
- @overload
96+
def removeprefix(self, prefix: str, /) -> str: ... # type: ignore[misc]
97+
- @overload
98+
- def removesuffix(self: LiteralString, suffix: LiteralString, /) -> LiteralString: ...
99+
- @overload
100+
def removesuffix(self, suffix: str, /) -> str: ... # type: ignore[misc]
102101
def rfind(self, sub: str, start: SupportsIndex | None = ..., end: SupportsIndex | None = ..., /) -> int: ...
103102
def rindex(self, sub: str, start: SupportsIndex | None = ..., end: SupportsIndex | None = ..., /) -> int: ...
104103
- @overload
@@ -151,7 +150,7 @@ index 63c53a5f6..d55042b56 100644
151150
def zfill(self, width: SupportsIndex, /) -> str: ... # type: ignore[misc]
152151
@staticmethod
153152
@overload
154-
@@ -581,39 +501,21 @@ class str(Sequence[str]):
153+
@@ -595,39 +515,21 @@ class str(Sequence[str]):
155154
@staticmethod
156155
@overload
157156
def maketrans(x: str, y: str, z: str, /) -> dict[int, int | None]: ...
@@ -193,5 +192,5 @@ index 63c53a5f6..d55042b56 100644
193192
def __getnewargs__(self) -> tuple[str]: ...
194193

195194
--
196-
2.47.0
195+
2.49.0
197196

0 commit comments

Comments
 (0)