-
Notifications
You must be signed in to change notification settings - Fork 91
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
style(datasets): use PEP 585 #191
Conversation
What am I doing wrong @deepyaman @astrojuanlu I've changed Union[plt.figure, List[plt.figure], Dict[str, plt.figure]] to Union[plt.figure, list[plt.figure], dict[str, plt.figure]] (and added the import But now the Python 3.7 and 3.8 tests are failing with: tests/matplotlib/test_matplotlib_writer.py:12: in <module>
from kedro_datasets.matplotlib import MatplotlibWriter
kedro_datasets/matplotlib/__init__.py:8: in <module>
from .matplotlib_writer import MatplotlibWriter
kedro_datasets/matplotlib/matplotlib_writer.py:24: in <module>
Union[plt.figure, list[plt.figure], dict[str, plt.figure]], NoReturn
E TypeError: 'type' object is not subscriptable |
Uh, sorry for sending you through this rabbit hole @merelcht. It seems like there's a common misconception that these annotations are usable in Python 3.7 and 3.8...
I think we have to park this until we drop support for Python 3.8. |
I managed to get it working okay on the core Kedro repo. Just the read the docs builds complaining there. |
😳 Well, I'm puzzled now. |
Same, I thought this repo would be much more straightforward.. |
...heh. @leycec has been summoned! Exhausting block of monolithic text now follows. Oh – and thanks so much for pinging me here, @astrojuanlu. Kedro looks amazing, everybody. I have two concrete suggestions with respect to PEP 585 under Python < 3.9. Something is guaranteed to work if you repeatedly bash it hard enough:
# Import the requisite machinery. If lucky, then worky.
from typing_extensions import Dict, List, Union
#from beartype.typing import Dict, List, Union # <-- alternate timeline approach
# @leycec guarantees success. Would he lie?
Union[plt.figure, List[plt.figure], Dict[str, plt.figure]]
|
Thanks a lot for chiming in! It's still not clear to me though why things are failing here while working normally over kedro-org/kedro#2540, and also how your advice helps us leverage PEP 585 proper, namely using native collections. We'll have to do a bit more digging I suppose. |
Likewise...
That is a noble goal. Sadly, you can't really achieve that noble goal under Python < 3.9 without breaking runtime things. Thankfully, while noble, that isn't really a useful goal. Right? PEP 585 is basically just syntactic sugar; it doesn't actually gain you anything tangible. In fact, mypy itself now strongly encourages use of
from collections.abc import Sequence
x: list[str]
y: dict[int, str]
z: Sequence[str] = x
Note the critical statement: "this will not be supported by the Python interpreter at runtime". In other words, what you are currently trying to do is likely to raise exceptions under Python < 3.9 from things that introspect type hints at runtime (e.g., |
I'm going to close this for now as it doesn't seem easy to enforce PEP 585 everywhere and it doesn't add a lot other than syntactic sugar. |
Well, in fact I think #442 already does this 😄 |
Description
Follow up on #190
Development notes
https://peps.python.org/pep-0585/
Checklist
RELEASE.md
file