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

When to set runtime-typing to no? #4545

Closed
nth10sd opened this issue Jun 7, 2021 · 2 comments · Fixed by #4549
Closed

When to set runtime-typing to no? #4545

nth10sd opened this issue Jun 7, 2021 · 2 comments · Fixed by #4549

Comments

@nth10sd
Copy link

nth10sd commented Jun 7, 2021

Question

For runtime-typing, it is mentioned to Set to no if the app / libary does NOT need to support runtime introspection of type annotations. May I clarify what this means? I have from __future__ import annotations turned on throughout, but it does not seem to make a difference.

(Reason: I have Optional[str] usages and I realised that pylint only advises to turn them to str | None only if runtime-typing is a non-default no - I would have thought they should be turned to str | None as long as we have from __future__ import annotations)

Sidenote: minor typo here, libary -> library.

@cdce8p
Copy link
Member

cdce8p commented Jun 7, 2021

Thanks for the question. The most common examples for runtime introspection are typing.get_type_hints and eval. Besides that there are libraries that use type annotations to generate code. If you rely on annotations just for type checking, you are probably fine.

Some more information: https://mypy.readthedocs.io/en/stable/runtime_troubles.html#annotation-issues-at-runtime

However, just using the default (runtime-typing=yes) is definitely the safer option. I would even go as far as to say, only set it to no if you already know what it means.

I would have thought they should be turned to str | None as long as we have from future import annotations

My intention was to only suggest it when it's truly safe to do so as it is a stylistic choice anyway. Optional and Union haven't been deprecated. Someone might have added from __future__ import annotations to solve a forward reference or something else without realizing the actual impact.

@nth10sd
Copy link
Author

nth10sd commented Jun 8, 2021

This does help, thank you!

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

Successfully merging a pull request may close this issue.

3 participants