Skip to content

Best Practices: Add note about object/Any #1198

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

Merged
merged 3 commits into from
Jun 2, 2022

Conversation

srittau
Copy link
Collaborator

@srittau srittau commented May 25, 2022

Part of #851

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

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

I'm not sure I'd recommend using from __future__ import annotations for new syntax without at least having a note mentioning that doing so breaks runtime uses of annotations (and e.g. type aliases won't work).

The note doesn't need to be lengthy or scary, e.g. it could just link to https://mypy.readthedocs.io/en/stable/runtime_troubles.html or something, but I do think we should acknowledge sharp edges.

@srittau
Copy link
Collaborator Author

srittau commented May 25, 2022

I'm not sure I'd recommend using from future import annotations for new syntax without at least having a note mentioning that doing so breaks runtime uses of annotations (and e.g. type aliases won't work).

I'm not sure I follow. What runtime uses break that work without the future import. The mypy docs link doesn't mention anything either.

Edit: Or even better yet, propose a wording.

Co-authored-by: Jelle Zijlstra <[email protected]>
@JelleZijlstra
Copy link
Member

I personally would not recommend using from __future__ import annotations:

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented May 25, 2022

It's not just the edge cases, since we're currently recommending from __future__ import annotations specifically as a way to backport new syntax (not just the usual forward references stuff). So typing.get_type_hints and friends will break.

Suggested wording (based on mypy docs):

Some code may evaluate annotations at runtime, for instance, through use of typing.get_type_hints or eval. If your annotation would raise an error when evaluated (say by using new-style unions on Python 3.9 and older), you may need to be careful. See here for more discussion on sharp edges.

Of course, if we don't want to recommend future annotations at all because of the edge cases and co_annotations stuff, this is moot.

@srittau srittau changed the title Best Practices: Add future import and object/Any Best Practices: Add note about object/Any May 27, 2022
@srittau
Copy link
Collaborator Author

srittau commented May 27, 2022

I have removed the future import section for now.

@srittau
Copy link
Collaborator Author

srittau commented Jun 2, 2022

Bumpity bump.

@JelleZijlstra JelleZijlstra merged commit 45dee4f into python:master Jun 2, 2022
@srittau srittau deleted the best-practices branch June 3, 2022 07:24
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.

3 participants