Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

type annotation in DCSample class #321

Open
prjemian opened this issue Jan 12, 2024 · 3 comments · May be fixed by #347
Open

type annotation in DCSample class #321

prjemian opened this issue Jan 12, 2024 · 3 comments · May be fixed by #347
Assignees
Labels
Milestone

Comments

@prjemian
Copy link
Contributor

Regarding

hklpy/hkl/configuration.py

Lines 248 to 249 in 32e3043

# TODO: Once py38 is dropped, re-enable the default value setting
U: List[List[float]] # = field(default_factory=list[list[float]])

Is py38 the issue, or is it because DCReflection is not a dataclass?

I believe the default_factory field should be a zero-argument callable, such as...

default_factory=lambda: [[]]  # list containing an empty list

Originally posted by @padraic-shafer in #317 (comment)

@prjemian prjemian added the bug label Jan 12, 2024
@prjemian prjemian added this to the v1.1.1 milestone Jan 12, 2024
@prjemian prjemian self-assigned this May 10, 2024
@prjemian prjemian modified the milestones: v1.1.1, v1.1.2 Aug 7, 2024
@prjemian
Copy link
Contributor Author

Is py38 the issue, or is it because DCReflection is not a dataclass?

Now, with Py3.12, can check that with pytest -vvv --lf hkl/tests/test_configuration.py -k test_restore:

code pass/fail
U: List[List[float]] pass
U: field(default_factory=lambda: [[]]) fail

Same error as before:

    def test_restore(e4cv_renamed, k4cv):
        config = DiffractometerConfiguration(e4cv_renamed)
>       before = config.export("dict")

hkl/tests/test_configuration.py:37: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
hkl/configuration.py:539: in export
    data = getattr(self, f"to_{fmt}")()
hkl/configuration.py:803: in to_dict
    return serialize(DCConfiguration, self.model)
hkl/configuration.py:744: in model
    obj = deserialize(DCConfiguration, data)  # also validates structure
/local/envs/dev-hklpy/lib/python3.12/site-packages/apischema/deserialization/__init__.py:887: in deserialize
    return deserialization_method(
...
... many stack entries omitted ...
...
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <apischema.deserialization.DeserializationMethodVisitor object at 0x7f9a3b797d40>
tp = Field(name=None,type=None,default=<dataclasses._MISSING_TYPE object at 0x7f9a9f4489e0>,default_factory=<function DCSample.<lambda> at 0x7f9a65f6a020>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=<dataclasses._MISSING_TYPE object at 0x7f9a9f4489e0>,_field_type=None)

    def unsupported(self, tp: AnyType) -> Result:
>       raise Unsupported(tp)
E       apischema.visitor.Unsupported: Field(name=None,type=None,default=<dataclasses._MISSING_TYPE object at 0x7f9a9f4489e0>,default_factory=<function DCSample.<lambda> at 0x7f9a65f6a020>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=<dataclasses._MISSING_TYPE object at 0x7f9a9f4489e0>,_field_type=None)

/local/envs/dev-hklpy/lib/python3.12/site-packages/apischema/visitor.py:151: Unsupported
============================================================== short test summary info ===============================================================
FAILED hkl/tests/test_configuration.py::test_restore - apischema.visitor.Unsupported: Field(name=None,type=None,default=<dataclasses._MISSING_TYPE object at 0x7f9a9f4489e0>,default_factory=<function DCSample.<lambda> at 0x7f9a65f6a020>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=<dataclasses._MISSING_TYPE object at 0x7f9a9f4489e0>,_field_type=None)
========================================================= 1 failed, 197 deselected in 0.41s ==========================================================

@prjemian
Copy link
Contributor Author

Is py38 the issue, or is it because DCReflection is not a dataclass?

Neither. Tested the same with Py3.11 & 3.12. This code is in DCSample. Assume that's where the problem lies.

prjemian added a commit that referenced this issue Jan 31, 2025
prjemian added a commit that referenced this issue Jan 31, 2025
@prjemian prjemian changed the title type annotation in DCReflection class type annotation in DCSample class Jan 31, 2025
@prjemian
Copy link
Contributor Author

Note that both DCReflection and DCSample are dataclasses:

hklpy/hkl/configuration.py

Lines 188 to 189 in 4bc42df

@dataclass
class DCReflection:

hklpy/hkl/configuration.py

Lines 228 to 229 in 4bc42df

@dataclass
class DCSample:

@prjemian prjemian linked a pull request Jan 31, 2025 that will close this issue
prjemian added a commit that referenced this issue Feb 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant