lovasoa / marshmallow_dataclass

Automatic generation of marshmallow schemas from dataclasses.
https://lovasoa.github.io/marshmallow_dataclass/html/marshmallow_dataclass.html
MIT License
444 stars 77 forks source link

Typeguard fails with 8.7.0 #272

Open epenet opened 3 weeks ago

epenet commented 3 weeks ago

It worked fine with 8.6.1 See https://github.com/hacf-fr/renault-api/pull/1234

<frozen importlib._bootstrap>:935: in _load_unlocked
    ???
.nox/typeguard-3-12/lib/python3.12/site-packages/typeguard/_importhook.py:98: in exec_module
    super().exec_module(module)
.nox/typeguard-3-12/lib/python3.12/site-packages/renault_api/kamereon/models.py:15: in <module>
    from . import helpers
<frozen importlib._bootstrap>:1360: in _find_and_load
    ???
<frozen importlib._bootstrap>:1331: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:935: in _load_unlocked
    ???
.nox/typeguard-3-12/lib/python3.12/site-packages/typeguard/_importhook.py:98: in exec_module
    super().exec_module(module)
<frozen importlib._bootstrap_external>:991: in exec_module
    ???
<frozen importlib._bootstrap_external>:1129: in get_code
    ???
.nox/typeguard-3-12/lib/python3.12/site-packages/typeguard/_importhook.py:87: in source_to_code
    return _call_with_frames_removed(
.nox/typeguard-3-12/lib/python3.12/site-packages/typeguard/_importhook.py:47: in _call_with_frames_removed
    return f(*args, **kwargs)
E     File "/home/runner/work/renault-api/renault-api/.nox/typeguard-3-12/lib/python3.12/site-packages/renault_api/kamereon/helpers.py", line 3
E       from __future__ import annotations
E       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E   SyntaxError: from __future__ imports must occur at the beginning of the file
dairiki commented 3 weeks ago

My guess is that this is more of a typeguard vs renault-api issue.

Previous to marshmallow_dataclass 8.7.0, typeguard was pinned to < 3. 8.7.0 relaxes that pin to allow for typeguard 4.x. (#250, #260) The new behavior appears to be caused by the switch to typeguard 4.

From the traceback, this appears to have something to do with the use of typeguards import hook. In your case, the import hook appears to be installed through the use of the typeguard's pytest plugin.

epenet commented 3 weeks ago

OK - I'm making progress, but I am still confused. It seems that the CI with marshmallow_dataclass 8.6.1, typeguard gets installed with version 4.3.0 But with marshmallow_dataclass 8.7.0, typeguard gets downgraded to version 4.0.1

$ pip show typeguard
Name: typeguard
Version: 4.3.0
$ pip show marshmallow-dataclass
Name: marshmallow_dataclass
Version: 8.6.1
$ pip show typing-inspect
Name: typing-inspect
Version: 0.8.0
==> OK

vs

$ pip show typeguard
Name: typeguard
Version: 4.0.1
$ pip show marshmallow-dataclass
Name: marshmallow_dataclass
Version: 8.7.0
$ pip show typing-inspect
Name: typing-inspect
Version: 0.9.0
==> SyntaxError: from __future__ imports must occur at the beginning of the file
epenet commented 3 weeks ago

OK - it seems the issue does come from marshmallow-dataclass, which restricts:

https://github.com/lovasoa/marshmallow_dataclass/blob/4edbfb44307b4df91261c75175d6916acd88f0be/setup.py#L49-L55

Should then not be loosened up?

    install_requires=[
        "marshmallow>=3.18.0,",
        "typing-inspect>=0.9.0",
        "typeguard>=4.0.0",
        # Need `dataclass_transform(field_specifiers)`
        "typing-extensions>=4.2.0; python_version<'3.11'",
    ]