tamuhey / dataclass_utils Goto Github PK
View Code? Open in Web Editor NEWRuntime typechecker for dataclass
License: Apache License 2.0
Runtime typechecker for dataclass
License: Apache License 2.0
I have no idea to support this feature
Hi, I've been using your library, loving it so far, but I've run into an issue:
I have a dataclass which has a tuple of integers.
I don't know how long the tuple will be at run time.
from dataclasses import dataclass
from dataclass_utils import check_type
from typing import Tuple
@dataclass
class Foo:
bar: Tuple[int,...] = (1,2,3)
check_type(Foo())
According to python's typing rules, this should pass, but it doesn't:
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm Community Edition 2022.1\plugins\python-ce\helpers\pydev\pydevd.py", line 1491, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm Community Edition 2022.1\plugins\python-ce\helpers\pydev_pydev_imps_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "D:/projects/fastpbrl-main/scripts/scratchpads/check_types.py", line 8, in
check_type(Foo())
File "D:\Users\agimg\Miniconda3\envs\clockwork_mind\lib\site-packages\dataclass_utils\type_checker.py", line 181, in check_root
raise err
dataclass_utils.error.Error0: Error in field 'bar'. Expected type typing.Tuple[int, ...], got <class 'tuple'> (value: (1, 2, 3))
python-BaseException
I have this definition:
@dataclass
class Detail
class DetailType(Enum):
MOVIE = 'movie'
MOVIE_BUNDLE = 'movie_bundle'
TV_SHOW = 'tv_show'
TV_SEASON = 'tv_season'
and then:
detail_type: DetailType = ''
the above fails because the library doesn't know DetailType
This removes the previous error, but a value of type str
is allowed:
detail_type: DetailItem.DetailType = ''
Hi there, I ran into another bug while using this:
I'm using python 3.10 which has some syntax sugar for creating unions using the | operator from https://peps.python.org/pep-0604/
Interestingly, this causes the type checking to fail, when using the old way of creating a typing.Union
object doesn't:
Here's the base case:
import dataclasses
import dataclass_utils
import typing
@dataclasses.dataclass
class Foo:
bar: typing.Union[str, int] = "bar"
f = Foo()
dataclass_utils.check_type(f)
print(f"Pass! {f=}")
This passes as expected:
/home/agimg/anaconda3/envs/pbrl/bin/python /home/agimg/projects/fastpbrl/scripts/scratchpads/dataclass_utils_bug_2.py
Pass! f=Foo(bar='bar')Process finished with exit code 0
But using the syntax sugar:
import dataclasses
import dataclass_utils
@dataclasses.dataclass
class FooWithSugar:
bar: str | int = "bar"
f_sugar = FooWithSugar()
dataclass_utils.check_type(f_sugar)
print(f"Pass! {f_sugar=}")
Fails with:
/home/agimg/anaconda3/envs/pbrl/bin/python /home/agimg/projects/fastpbrl/scripts/scratchpads/dataclass_utils_bug_2.py
Traceback (most recent call last):
File "/home/agimg/projects/fastpbrl/scripts/scratchpads/dataclass_utils_bug_2.py", line 25, in
dataclass_utils.check_type(f_sugar)
File "/home/agimg/anaconda3/envs/pbrl/lib/python3.10/site-packages/dataclass_utils/type_checker.py", line 189, in check_root
raise err
dataclass_utils.error.Error0: Error in field 'bar'. Expected type <class 'types.UnionType'>, got <class 'str'> (value: bar)Process finished with exit code 1
Do you plan on supporting this new syntax?
Hello, I ran into a bug similar to #8
If i try using the python 3.10 union syntax sugar on dataclass type hints then try to call dataclass_utils.into on them, it breaks, but doing so with a normal typing union doesn't even though they should be equivalent
import dataclasses
import dataclass_utils
import typing
print(int | str)
print(typing.Union[int, str])
@dataclasses.dataclass
class FooUnion:
foo: typing.Union[str, int] = "foo"
@dataclasses.dataclass
class FooSugar:
foo: str | int = "foo"
f_union = FooUnion()
f_dict = dataclasses.asdict(f_union)
f_union_from_dict = dataclass_utils.into(f_dict, FooUnion)
print(f"Pass! {f_union_from_dict=}")
f_sugar = FooSugar()
f_dict = dataclasses.asdict(f_sugar)
f_sugar_from_dict = dataclass_utils.into(f_dict, FooSugar)
print(f"Pass! {f_sugar_from_dict=}")
Traceback (most recent call last):
File "[...]\dataclass_utils_bug.py", line 23, in
f_sugar_from_dict = dataclass_utils.into(f_dict, FooSugar)
File "[...]\lib\site-packages\dataclass_utils\into_dataclass.py", line 24, in into_root
raise ret
dataclass_utils.error.Error0: Error in field 'foo'. Expected type str | int, got <class 'str'> (value: foo)
int | str
typing.Union[int, str]
Pass! f_union_from_dict=FooUnion(foo='foo')Process finished with exit code 1
Since poetry automatically puts an upper bound on dependency versions, typing-extensions
is limited to <4.0
as well as >=3.10
. This means that one cannot use a recent typing-extensions
when using dataclass-utils
, while typing-extensions
4.2 still supports python 3.7 which is already the minimum python version for dataclass-utils
anyway.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.