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

Fix typing for redis #3118

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

Fix typing for redis #3118

wants to merge 2 commits into from

Conversation

lzchen
Copy link
Contributor

@lzchen lzchen commented Dec 18, 2024

Followup to #3110

I believe Pep 0604 which introduces union typing syntax is only supported for Python 3.10+. Since all instrumentations still support 3.8+, I believe we still need to adhere to the explicit Optional and Union typings.

Followup to #3110

I believe [Pep 0604](https://peps.python.org/pep-0604/) which introduces union typing syntax is only supported for Python 3.10+.
Since all instrumentations still support 3.8+, I believe we still need to adhere to the explicit `Optional` and `Union` typings.
@lzchen lzchen marked this pull request as ready for review December 18, 2024 17:45
@lzchen lzchen added the Skip Changelog PRs that do not require a CHANGELOG.md entry label Dec 18, 2024
Copy link
Contributor

@Kludex Kludex left a comment

Choose a reason for hiding this comment

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

@lzchen This is not needed.

The from __future__ import annotations on the first line makes sure Python will not look at the type hints at runtime - Which allow us to use type hints from more modern versions.

Also... The pipeline for Python 3.8 and 3.9 is passing... 😅

@samuelcolvin
Copy link
Contributor

samuelcolvin commented Dec 19, 2024

@lzchen this change is not necessary.

As of pep-0563 introduced in Python 3.7, the magic import

from __future__ import annotations

causes Python to not evaluate type hints, this in turn means we can use new syntax like | unions even with older versions of Python. Static type checkers like mypy and pyright can still read and interpret these type annotations, so there's no drawback.

Many popular python packages like Pydantic, FastAPI, rich, cryptography use this technique.

Even Microsoft (who I believe you work for) make extensive use of from __future__ import annotations to adopt modern typing while supporting older versions of python:

In the most downloaded python package from Microsoft azure-core, there are 82 references to from __future__ import annotations, indeed azure-sdk-for-python/doc/dev /static_type_checking.md includes this:

*Note: Supported in Python 3.9+. For <3.9, You must include from __future__ import annotations import to be able to pass in generic list[str] as a type hint rather than typing.List[str].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Skip Changelog PRs that do not require a CHANGELOG.md entry
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants