fix mypy errors
this fixes two distinct mypy errors one where NamedTuple/dataclassees can't be defined locally https://github.com/python/mypy/issues/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
This commit is contained in:
parent
2bbbff0021
commit
1cdef6f40a
4 changed files with 31 additions and 26 deletions
|
@ -90,7 +90,7 @@ def entries() -> Iterable[Entry]:
|
||||||
|
|
||||||
def fill_influxdb() -> None:
|
def fill_influxdb() -> None:
|
||||||
from .core.influxdb import magic_fill
|
from .core.influxdb import magic_fill
|
||||||
from .core.types import Freezer
|
from .core.freezer import Freezer
|
||||||
freezer = Freezer(Entry)
|
freezer = Freezer(Entry)
|
||||||
fit = (freezer.freeze(e) for e in entries())
|
fit = (freezer.freeze(e) for e in entries())
|
||||||
# TODO crap, influxdb doesn't like None https://github.com/influxdata/influxdb/issues/7722
|
# TODO crap, influxdb doesn't like None https://github.com/influxdata/influxdb/issues/7722
|
||||||
|
|
|
@ -2,7 +2,7 @@ from .common import assert_subpackage; assert_subpackage(__name__)
|
||||||
|
|
||||||
import dataclasses as dcl
|
import dataclasses as dcl
|
||||||
import inspect
|
import inspect
|
||||||
from typing import TypeVar, Type
|
from typing import TypeVar, Type, Any
|
||||||
|
|
||||||
D = TypeVar('D')
|
D = TypeVar('D')
|
||||||
|
|
||||||
|
@ -40,10 +40,11 @@ class Freezer(Generic[D]):
|
||||||
|
|
||||||
### tests
|
### tests
|
||||||
|
|
||||||
def test_freezer() -> None:
|
|
||||||
from typing import Any
|
# this needs to be defined here to prevent a mypy bug
|
||||||
@dcl.dataclass
|
# see https://github.com/python/mypy/issues/7281
|
||||||
class A:
|
@dcl.dataclass
|
||||||
|
class _A:
|
||||||
x: Any
|
x: Any
|
||||||
|
|
||||||
# TODO what about error handling?
|
# TODO what about error handling?
|
||||||
|
@ -55,8 +56,11 @@ def test_freezer() -> None:
|
||||||
def untyped(self):
|
def untyped(self):
|
||||||
return self.x['an_any']
|
return self.x['an_any']
|
||||||
|
|
||||||
val = A(x=dict(an_int=123, an_any=[1, 2, 3]))
|
|
||||||
af = Freezer(A)
|
def test_freezer() -> None:
|
||||||
|
|
||||||
|
val = _A(x=dict(an_int=123, an_any=[1, 2, 3]))
|
||||||
|
af = Freezer(_A)
|
||||||
fval = af.freeze(val)
|
fval = af.freeze(val)
|
||||||
|
|
||||||
fd = vars(fval)
|
fd = vars(fval)
|
|
@ -1,5 +1,5 @@
|
||||||
import datetime
|
import datetime
|
||||||
from typing import Any, Optional, Callable
|
from typing import Any, Optional, Callable, NamedTuple
|
||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
|
|
||||||
from .common import is_namedtuple
|
from .common import is_namedtuple
|
||||||
|
@ -137,17 +137,18 @@ def test_serialize_fallback() -> None:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# this needs to be defined here to prevent a mypy bug
|
||||||
|
# see https://github.com/python/mypy/issues/7281
|
||||||
|
class _A(NamedTuple):
|
||||||
|
x: int
|
||||||
|
y: float
|
||||||
|
|
||||||
|
|
||||||
def test_nt_serialize() -> None:
|
def test_nt_serialize() -> None:
|
||||||
import json as jsn # dont cause possible conflicts with module code
|
import json as jsn # dont cause possible conflicts with module code
|
||||||
import orjson # import to make sure this is installed
|
import orjson # import to make sure this is installed
|
||||||
|
|
||||||
from typing import NamedTuple
|
res: str = dumps(_A(x=1, y=2.0))
|
||||||
|
|
||||||
class A(NamedTuple):
|
|
||||||
x: int
|
|
||||||
y: float
|
|
||||||
|
|
||||||
res: str = dumps(A(x=1, y=2.0))
|
|
||||||
assert res == '{"x":1,"y":2.0}'
|
assert res == '{"x":1,"y":2.0}'
|
||||||
|
|
||||||
# test orjson option kwarg
|
# test orjson option kwarg
|
||||||
|
|
|
@ -16,6 +16,6 @@ from my.core.core_config import *
|
||||||
from my.core.error import *
|
from my.core.error import *
|
||||||
from my.core.util import *
|
from my.core.util import *
|
||||||
from my.core.discovery_pure import *
|
from my.core.discovery_pure import *
|
||||||
from my.core.types import *
|
from my.core.freezer import *
|
||||||
from my.core.stats import *
|
from my.core.stats import *
|
||||||
from my.core.serialize import test_serialize_fallback
|
from my.core.serialize import test_serialize_fallback
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue