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

Support __futre__.annotations #51

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

ymd-h
Copy link

@ymd-h ymd-h commented Jun 6, 2023

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: #47


I tested with tox -e . on Python:3.10 docker container.

When importing __future__.annotations, class annotations are evaluated
lazily, and dataclasses.Field.type becomes str instead of actual type.

To access its accutual type, we patch the member.

Ref: mivade#47
Copy link
Owner

@mivade mivade left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll want to somehow add tests for this.

argparse_dataclass.py Outdated Show resolved Hide resolved
argparse_dataclass.py Show resolved Hide resolved
@ymd-h
Copy link
Author

ymd-h commented Jun 7, 2023

@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.

@ymd-h
Copy link
Author

ymd-h commented Jun 8, 2023

@mivade

As far as I checked[*1], from __future__ import annotations affects only inside the module,
so that adding new tests by bbc24de 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)
SS 2023-06-08 18 15 33


[*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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants