Skip to content

Commit 2a7e1eb

Browse files
committed
fix mypy errors
this fixes two distinct mypy errors one where NamedTuple/dataclassees can't be defined locally python/mypy#7281 which happens when you run mypy like mypy -p my.core on warm cache the second error is the core/types.py file shadowing the stdlib types module
1 parent 2bbbff0 commit 2a7e1eb

File tree

4 files changed

+32
-27
lines changed

4 files changed

+32
-27
lines changed

my/arbtt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def entries() -> Iterable[Entry]:
9090

9191
def fill_influxdb() -> None:
9292
from .core.influxdb import magic_fill
93-
from .core.types import Freezer
93+
from .core.freezer import Freezer
9494
freezer = Freezer(Entry)
9595
fit = (freezer.freeze(e) for e in entries())
9696
# TODO crap, influxdb doesn't like None https://github.com/influxdata/influxdb/issues/7722

my/core/types.py renamed to my/core/freezer.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import dataclasses as dcl
44
import inspect
5-
from typing import TypeVar, Type
5+
from typing import TypeVar, Type, Any
66

77
D = TypeVar('D')
88

@@ -40,23 +40,27 @@ def freeze(self, value: D) -> D:
4040

4141
### tests
4242

43+
44+
# this needs to be defined here to prevent a mypy bug
45+
# see https://github.com/python/mypy/issues/7281
46+
@dcl.dataclass
47+
class _A:
48+
x: Any
49+
50+
# TODO what about error handling?
51+
@property
52+
def typed(self) -> int:
53+
return self.x['an_int']
54+
55+
@property
56+
def untyped(self):
57+
return self.x['an_any']
58+
59+
4360
def test_freezer() -> None:
44-
from typing import Any
45-
@dcl.dataclass
46-
class A:
47-
x: Any
48-
49-
# TODO what about error handling?
50-
@property
51-
def typed(self) -> int:
52-
return self.x['an_int']
53-
54-
@property
55-
def untyped(self):
56-
return self.x['an_any']
57-
58-
val = A(x=dict(an_int=123, an_any=[1, 2, 3]))
59-
af = Freezer(A)
61+
62+
val = _A(x=dict(an_int=123, an_any=[1, 2, 3]))
63+
af = Freezer(_A)
6064
fval = af.freeze(val)
6165

6266
fd = vars(fval)

my/core/serialize.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import datetime
2-
from typing import Any, Optional, Callable
2+
from typing import Any, Optional, Callable, NamedTuple
33
from functools import lru_cache
44

55
from .common import is_namedtuple
@@ -137,17 +137,18 @@ def test_serialize_fallback() -> None:
137137

138138

139139

140+
# this needs to be defined here to prevent a mypy bug
141+
# see https://github.com/python/mypy/issues/7281
142+
class _A(NamedTuple):
143+
x: int
144+
y: float
145+
146+
140147
def test_nt_serialize() -> None:
141148
import json as jsn # dont cause possible conflicts with module code
142149
import orjson # import to make sure this is installed
143150

144-
from typing import NamedTuple
145-
146-
class A(NamedTuple):
147-
x: int
148-
y: float
149-
150-
res: str = dumps(A(x=1, y=2.0))
151+
res: str = dumps(_A(x=1, y=2.0))
151152
assert res == '{"x":1,"y":2.0}'
152153

153154
# test orjson option kwarg

tests/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616
from my.core.error import *
1717
from my.core.util import *
1818
from my.core.discovery_pure import *
19-
from my.core.types import *
19+
from my.core.freezer import *
2020
from my.core.stats import *
2121
from my.core.serialize import test_serialize_fallback

0 commit comments

Comments
 (0)