You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Normally you can just pass a type to a terse match, but currently it doesn't work if the type is a dataclass because it tries to match against the type as if it were an instance of the dataclass. https://docs.python.org/3/library/dataclasses.html#dataclasses.is_dataclass recommends using is_dataclass(obj) and not isinstance(obj, type) to only match instances.
Example showing the bug:
In [1]: fromdataclassesimportdataclassIn [2]: @dataclass
...: classFoo:
...: x: int
...:
In [3]: fromapmimport*In [4]: match(Foo(1), Foo, 1) # <--- should work but doesn't---------------------------------------------------------------------------MatchErrorTraceback (mostrecentcalllast)
<ipython-input-4-58e7e898a729>in<module>---->1match(Foo(1), Foo, 1)
~/.local/lib/python3.9/site-packages/apm/match.pyinmatch(value, pattern, multimatch, strict, captureall, *extra)
83returninvoke(acc[0], [])
84returnacc[0]
--->85raiseMatchError(value)
8687ifisinstance(captureall, dict):
MatchError: Foo(x=1)
In [5]: match(Foo(1), InstanceOf(Foo), 1) # <--- workaroundOut[5]: 1
The text was updated successfully, but these errors were encountered:
awesome-pattern-matching/apm/core.py
Lines 220 to 225 in a6dfd4a
Normally you can just pass a type to a terse match, but currently it doesn't work if the type is a dataclass because it tries to match against the type as if it were an instance of the dataclass. https://docs.python.org/3/library/dataclasses.html#dataclasses.is_dataclass recommends using
is_dataclass(obj) and not isinstance(obj, type)
to only match instances.Example showing the bug:
The text was updated successfully, but these errors were encountered: