Skip to content

Commit 60c1c7f

Browse files
committed
Add content for major changes and other smaller updates
1 parent 4a773d7 commit 60c1c7f

File tree

1 file changed

+72
-19
lines changed

1 file changed

+72
-19
lines changed

CHANGELOG.md

Lines changed: 72 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,94 @@
66

77
### Different Property Getter and Setter Types
88

9-
Mypy now supports using different types for property getter and setter.
9+
Mypy now supports using different types for a property getter and setter:
1010

1111
```python
1212
class A:
13-
value: int
13+
_value: int
1414

1515
@property
16-
def f(self) -> int:
17-
return self.value
18-
@f.setter
19-
def f(self, x: str | int) -> None:
16+
def foo(self) -> int:
17+
return self._value
18+
19+
@foo.setter
20+
def foo(self, x: str | int) -> None:
2021
try:
21-
self.value = int(x)
22+
self._value = int(x)
2223
except ValueError:
23-
raise Exception(f"'{x}' is not a valid value for 'f'")
24+
raise Exception(f"'{x}' is not a valid value for 'foo'")
2425
```
25-
Contributed by Ivan Levkivskyi (PR [18510](https://github.com/python/mypy/pull/18510))
26+
This was contributed by Ivan Levkivskyi (PR [18510](https://github.com/python/mypy/pull/18510)).
2627

2728
### Flexible Variable Redefinitions (Experimental)
2829

29-
* Add flag to allow more flexible variable redefinition (Jukka Lehtosalo, PR [18727](https://github.com/python/mypy/pull/18727))
30+
Mypy now allows unannotated variables to be freely redefined with
31+
different types when using the experimental `--allow-redefinition-new`
32+
flag. You will also need to enable `--local-partial-types`. Mypy will
33+
now infer a union type when different types are assigned to a
34+
variable:
35+
36+
```py
37+
# mypy: allow-redefinition-new, local-partial-types
38+
39+
def f(n: int, b: bool) -> int | str:
40+
if b:
41+
x = n
42+
else:
43+
x = str(n)
44+
# Type of 'x' is int | str here.
45+
return x
46+
```
47+
48+
Without the new flag, mypy only supports inferring optional types (`X
49+
| None`) from multiple assignments, but now mypy can infer arbitrary
50+
union types.
51+
52+
An unannotated variable can now also have different types in different
53+
code locations:
54+
55+
```py
56+
# mypy: allow-redefinition-new, local-partial-types
57+
...
58+
59+
if cond():
60+
for x in range(n):
61+
# Type of 'x' is 'int' here
62+
...
63+
else:
64+
for x in ['a', 'b']:
65+
# Type of 'x' is 'str' here
66+
...
67+
```
68+
69+
We are planning to turn this flag on by default in mypy 2.0, along
70+
with `--local-partial-types`. The feature is still experimental and
71+
has known issues, and the semantics may still change in the
72+
future. You may need to update or add type annotations when switching
73+
to the new behavior, but if you encounter anything unexpected, please
74+
create a GitHub issue.
75+
76+
This was contributed by Jukka Lehtosalo (PR [18727](https://github.com/python/mypy/pull/18727)).
3077

3178
### Stricter Type Checking with Imprecise Types
3279

33-
TODO:
80+
Mypy can now detect additional errors in code that uses `Any` types or has missing function annotations.
3481

3582
* `dict.get`
3683
* Use union types instead of join in binder (Ivan Levkivskyi, PR [18538](https://github.com/python/mypy/pull/18538))
3784
* Check superclass compatibility of untyped methods if `--check-untyped-defs` is set (Stanislav Terliakov, PR [18970](https://github.com/python/mypy/pull/18970))
3885

3986
### Improvements to Attribute Resolution
4087

41-
This release includes various fixes to inconsistent resolution of attribute access.
88+
This release includes several fixes to inconsistent resolution of attribute, method and descriptor types.
4289

43-
* Consolidate descriptor handling in checkmember.py (Ivan Levkivskyi, PR [18831](https://github.com/python/mypy/pull/18831))
44-
* Use checkmember.py to check multiple inheritance (Ivan Levkivskyi, PR [18876](https://github.com/python/mypy/pull/18876))
45-
* Use checkmember.py to check method override (Ivan Levkivskyi, PR [18870](https://github.com/python/mypy/pull/18870))
90+
* Consolidate descriptor handling (Ivan Levkivskyi, PR [18831](https://github.com/python/mypy/pull/18831))
91+
* Make multiple inheritance checking use common semantics (Ivan Levkivskyi, PR [18876](https://github.com/python/mypy/pull/18876))
92+
* Make method override checking use common semantics (Ivan Levkivskyi, PR [18870](https://github.com/python/mypy/pull/18870))
4693
* Fix descriptor overload selection (Ivan Levkivskyi, PR [18868](https://github.com/python/mypy/pull/18868))
47-
* Handle union types when binding self (Ivan Levkivskyi, PR [18867](https://github.com/python/mypy/pull/18867))
48-
* Use checkmember.py to check variable overrides (Ivan Levkivskyi, PR [18847](https://github.com/python/mypy/pull/18847))
49-
* Consolidate descriptor handling in checkmember.py (Ivan Levkivskyi, PR [18831](https://github.com/python/mypy/pull/18831))
94+
* Handle union types when binding `self` (Ivan Levkivskyi, PR [18867](https://github.com/python/mypy/pull/18867))
95+
* Make variable override checking use common semantics (Ivan Levkivskyi, PR [18847](https://github.com/python/mypy/pull/18847))
96+
* Make descriptor handling behave consistently (Ivan Levkivskyi, PR [18831](https://github.com/python/mypy/pull/18831))
5097

5198
### Make Implementation for Abstract Overloads Optional
5299

@@ -61,20 +108,26 @@ This release includes various fixes to inconsistent resolution of attribute acce
61108
It's now possible to selectively disable warnings generated from
62109
[`warnings.deprecated`](https://docs.python.org/3/library/warnings.html#warnings.deprecated)
63110
using the [`--deprecated-calls-exclude`](https://mypy.readthedocs.io/en/stable/command_line.html#cmdoption-mypy-deprecated-calls-exclude)
64-
option.
111+
option:
65112

66113
```python
67114
# mypy --enable-error-code deprecated
68115
# --deprecated-calls-exclude=foo.A
69116
import foo
70117

71118
foo.A().func() # OK, the deprecated warning is ignored
119+
```
72120

121+
```python
73122
# file foo.py
123+
74124
from typing_extensions import deprecated
125+
75126
class A:
76127
@deprecated("Use A.func2 instead")
77128
def func(self): pass
129+
130+
...
78131
```
79132

80133
Contributed by Marc Mueller (PR [18641](https://github.com/python/mypy/pull/18641))

0 commit comments

Comments
 (0)