argparse_dataclass
argparse_dataclass copied to clipboard
Support __futre__.annotations
When importing __future__.annotations, class annotations are evaluated lazily, and dataclasses.Field.type becomes str instead of actual type.
To access its actual type, I patch the member.
Ref: https://github.com/mivade/argparse_dataclass/issues/47
I tested with tox -e . on Python:3.10 docker container.
@mivade Thank you for your reviewing.
We'll want to somehow add tests for this.
I agree with you.
Let me think how to test w/ and w/o annotations.
(I'm not sure whether once I import it, the effect spreads all over the tox tests or not.)
If you have good ideas, please let me know.
@mivade
As far as I checked[*1], from __future__ import annotations affects only inside the module,
so that adding new tests by https://github.com/mivade/argparse_dataclass/pull/51/commits/bbc24de1f77459c86375208fb68ad6cda55becf0 should preserve original tests as "without annotations".
I think test covers with and without annotations.
What I don't understand is CI test.
On my local machine with docker container (python:3.10),
all tox, tox -e ., tox -e py, and python -m doctest argparse_dataclass.py work fine[*2],
however, GitHub Actions fails;
https://github.com/mivade/argparse_dataclass/actions/runs/5204543406
I have no idea. Could you help me?
[*1]
The following is screenshot of check on Google Colab.
Cell 1 creates separate module with annotations.
Cell 2 imports the module, then in the module, field.type is 'str' (string object)
Cell 3 directly define class where I don't import annotations explicitly, then field.type is <class 'str'> (class object)
[*2]
root@812e48d520f9:/code# tox
py36: skipped because could not find python interpreter with spec(s): py36
py36: SKIP ⚠ in 0.1 seconds
py37: skipped because could not find python interpreter with spec(s): py37
py37: SKIP ⚠ in 0.05 seconds
py38: skipped because could not find python interpreter with spec(s): py38
py38: SKIP ⚠ in 0.06 seconds
.pkg: _optional_hooks> python /usr/local/lib/python3.10/site-packages/pyproject_api/_backend.py True setuptools.build_meta __legacy__
.pkg: get_requires_for_build_sdist> python /usr/local/lib/python3.10/site-packages/pyproject_api/_backend.py True setuptools.build_meta __legacy__
.pkg: prepare_metadata_for_build_wheel> python /usr/local/lib/python3.10/site-packages/pyproject_api/_backend.py True setuptools.build_meta __legacy__
.pkg: build_sdist> python /usr/local/lib/python3.10/site-packages/pyproject_api/_backend.py True setuptools.build_meta __legacy__
py39: install_package> python -I -m pip install --force-reinstall --no-deps /code/.tox/.tmp/package/20/argparse_dataclass-1.0.0.tar.gz
py39: commands[0]> python -m doctest argparse_dataclass.py
py39: commands[1]> pytest
============================================================================== test session starts ===============================================================================
platform linux -- Python 3.9.2, pytest-7.3.1, pluggy-1.0.0
cachedir: .tox/py39/.pytest_cache
rootdir: /code
collected 44 items
tests/test_annotations.py . [ 2%]
tests/test_argumentparser.py ..................... [ 50%]
tests/test_basic.py . [ 52%]
tests/test_functional.py ..................... [100%]
=============================================================================== 44 passed in 0.21s ===============================================================================
py39: commands_post[0]> black --check argparse_dataclass.py
All done! ✨ 🍰 ✨
1 file would be left unchanged.
py39: commands_post[1]> black --check tests
All done! ✨ 🍰 ✨
6 files would be left unchanged.
py39: OK ✔ in 6.26 seconds
py310: install_package> python -I -m pip install --force-reinstall --no-deps /code/.tox/.tmp/package/21/argparse_dataclass-1.0.0.tar.gz
py310: commands[0]> python -m doctest argparse_dataclass.py
py310: commands[1]> pytest
============================================================================== test session starts ===============================================================================
platform linux -- Python 3.10.11, pytest-7.3.1, pluggy-1.0.0
cachedir: .tox/py310/.pytest_cache
rootdir: /code
collected 44 items
tests/test_annotations.py . [ 2%]
tests/test_argumentparser.py ..................... [ 50%]
tests/test_basic.py . [ 52%]
tests/test_functional.py ..................... [100%]
=============================================================================== 44 passed in 0.20s ===============================================================================
py310: commands_post[0]> black --check argparse_dataclass.py
All done! ✨ 🍰 ✨
1 file would be left unchanged.
py310: commands_post[1]> black --check tests
All done! ✨ 🍰 ✨
6 files would be left unchanged.
.pkg: _exit> python /usr/local/lib/python3.10/site-packages/pyproject_api/_backend.py True setuptools.build_meta __legacy__
py36: SKIP (0.10 seconds)
py37: SKIP (0.05 seconds)
py38: SKIP (0.05 seconds)
py39: OK (6.26=setup[4.70]+cmd[0.15,0.77,0.30,0.34] seconds)
py310: OK (5.19=setup[3.60]+cmd[0.13,0.79,0.32,0.35] seconds)
congratulations :) (11.99 seconds)