Skip to content

Adds better type narrowing docs #11088

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 4 commits into from
Sep 19, 2021
Merged

Adds better type narrowing docs #11088

merged 4 commits into from
Sep 19, 2021

Conversation

sobolevn
Copy link
Member

List of changed:

  • I've renamed casts into type-narrowing
  • I've moved TypeGuard type there, it fits perfectly
  • I've added new docs about general type narrowing techniques
  • I've added / changed links where it was required

Refs #11014
CC @hauntsaninja


.. note::

Narrowing type for some impossible state will be treated as an error.
Copy link
Collaborator

Choose a reason for hiding this comment

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

This isn't generally true, e.g. not even with --strict, I think you need --warn-unreachable for this.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Could also maybe mention the assert_never trick here though

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.

This is awesome. I made a bunch of small wording changes. The only substantive comment here is about --warn-unreachable which we need to be a little careful about. E.g., there's some discussion of it here: https://mypy.readthedocs.io/en/stable/common_issues.html#unreachable-code

@sobolevn
Copy link
Member Author

@hauntsaninja thanks a lot for your help! I've added a note about --warn-unreachable

@hauntsaninja hauntsaninja merged commit b3ff2a6 into python:master Sep 19, 2021
@hauntsaninja
Copy link
Collaborator

Thank you!

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.

2 participants