Skip to content

Advising returning X | Any in documentation #925

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

Closed
Gobot1234 opened this issue Nov 5, 2021 · 2 comments
Closed

Advising returning X | Any in documentation #925

Gobot1234 opened this issue Nov 5, 2021 · 2 comments
Labels
topic: documentation Documentation-related issues and PRs

Comments

@Gobot1234
Copy link
Contributor

In the typing docs https://typing.readthedocs.io/en/latest/source/stubs.html#types it states:

"Avoid union return types, since they require isinstance() checks. Use Any or X | Any if necessary."

I really don't think this is a good advise. In cases like this I'm inclined to use overloads, handle each case separately or separate functionality into separate methods/functions. I'm interested as to whether or not you think this should be changed?

@JelleZijlstra
Copy link
Member

JelleZijlstra commented Nov 5, 2021

This advise mostly came out of typeshed where "separate functionality into separate methods" isn't an option (we can't change the stdlib). And overloads aren't always powerful enough either.

It would be useful though to recommend refactoring your code to avoid a Union return type if possible, or to use overloads where that makes sense.

@srittau srittau added the topic: documentation Documentation-related issues and PRs label Nov 5, 2021
@srittau
Copy link
Collaborator

srittau commented Nov 5, 2021

See #851 for the issue about creating a best practices document that is supposed to merge the existing best practices sections.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: documentation Documentation-related issues and PRs
Projects
None yet
Development

No branches or pull requests

3 participants