Skip to content

Commit d52cf49

Browse files
authored
Serialize additional attributes of "Any" objects (#4481)
1 parent 8082af2 commit d52cf49

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

mypy/types.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,12 +327,15 @@ def __eq__(self, other: object) -> bool:
327327
return isinstance(other, AnyType)
328328

329329
def serialize(self) -> JsonDict:
330-
return {'.class': 'AnyType'}
330+
return {'.class': 'AnyType', 'type_of_any': self.type_of_any.name,
331+
'source_any': self.source_any.serialize() if self.source_any is not None else None}
331332

332333
@classmethod
333334
def deserialize(cls, data: JsonDict) -> 'AnyType':
334335
assert data['.class'] == 'AnyType'
335-
return AnyType(TypeOfAny.special_form)
336+
source = data['source_any']
337+
return AnyType(TypeOfAny[data['type_of_any']],
338+
AnyType.deserialize(source) if source is not None else None)
336339

337340

338341
class UninhabitedType(Type):

test-data/unit/check-incremental.test

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3447,3 +3447,22 @@ import m # No error here
34473447
tmp/m/a.py:1: error: Unsupported operand types for + ("int" and "str")
34483448
[out2]
34493449
tmp/m/a.py:1: error: Unsupported operand types for + ("int" and "str")
3450+
3451+
[case testDisallowAnyExprIncremental]
3452+
# cmd: mypy -m main
3453+
# flags: --disallow-any-expr
3454+
3455+
[file ns.py]
3456+
class Namespace:
3457+
def __init__(self):
3458+
self.user = 0
3459+
3460+
[file main.py]
3461+
import ns
3462+
user = ns.Namespace.user
3463+
3464+
[out1]
3465+
tmp/main.py:2: error: Expression has type "Any"
3466+
3467+
[out2]
3468+
tmp/main.py:2: error: Expression has type "Any"

0 commit comments

Comments
 (0)