-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Document we're not tracking relationships between symbols #16018
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -3,7 +3,7 @@ | |||||
Type narrowing | ||||||
============== | ||||||
|
||||||
This section is dedicated to several type narrowing | ||||||
This section is dedicated to several type narrowing | ||||||
techniques which are supported by mypy. | ||||||
|
||||||
Type narrowing is when you convince a type checker that a broader type is actually more specific, for instance, that an object of type ``Shape`` is actually of the narrower type ``Square``. | ||||||
|
@@ -14,10 +14,11 @@ Type narrowing expressions | |||||
|
||||||
The simplest way to narrow a type is to use one of the supported expressions: | ||||||
|
||||||
- :py:func:`isinstance` like in ``isinstance(obj, float)`` will narrow ``obj`` to have ``float`` type | ||||||
- :py:func:`issubclass` like in ``issubclass(cls, MyClass)`` will narrow ``cls`` to be ``Type[MyClass]`` | ||||||
- :py:class:`type` like in ``type(obj) is int`` will narrow ``obj`` to have ``int`` type | ||||||
- :py:func:`callable` like in ``callable(obj)`` will narrow object to callable type | ||||||
- :py:func:`isinstance` like in :code:`isinstance(obj, float)` will narrow ``obj`` to have ``float`` type | ||||||
- :py:func:`issubclass` like in :code:`issubclass(cls, MyClass)` will narrow ``cls`` to be ``Type[MyClass]`` | ||||||
- :py:class:`type` like in :code:`type(obj) is int` will narrow ``obj`` to have ``int`` type | ||||||
- :py:func:`callable` like in :code:`callable(obj)` will narrow object to callable type | ||||||
- :code:`obj is not None` will narrow object to its :ref:`non-optional form <strict_optional>` | ||||||
|
||||||
Type narrowing is contextual. For example, based on the condition, mypy will narrow an expression only within an ``if`` branch: | ||||||
|
||||||
|
@@ -83,6 +84,38 @@ We can also use ``assert`` to narrow types in the same context: | |||||
reveal_type(x) # Revealed type is "builtins.int" | ||||||
print(x + '!') # Typechecks with `mypy`, but fails in runtime. | ||||||
|
||||||
|
||||||
Limitations of narrowing | ||||||
~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hope this is the right place. I wanted it to have a heading, to make it easy to refer people, but it's also a bit strange that it comes as a topic in the same hierarchy and right before "is_subclass" and "callable". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's make it a heading and move it to the bottom of the page. |
||||||
|
||||||
Mypy's analysis is limited to individual symbols and it will not track | ||||||
relationships between symbols. For example, in the following code | ||||||
it's easy to deduce that if :code:`a` is None then :code:`b` must not be, | ||||||
therefore :code:`a or b` will always be a string, but Mypy will not be able to tell that: | ||||||
|
||||||
.. code-block:: python | ||||||
|
||||||
def f(a: str | None, b: str | None) -> str: | ||||||
if a is not None or b is not None: | ||||||
return a or b # Incompatible return value type (got "str | None", expected "str") | ||||||
return 'spam' | ||||||
|
||||||
Tracking these sort of cross-variable conditions in a type checker would add significant complexity | ||||||
and performance overhead and would be computationally infeasible in all but the most basic cases. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Claim feels a little too strong, first half of the sentence says enough. |
||||||
|
||||||
You may override the type checker with a :ref:`cast <casts>`, or rewrite the function to be | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
When I run into this, assert is usually more ergonomic. It's also safer than cast. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably would phrase as "help the type checker with an cast: def f(a: str | None, b: str | None) -> str:
if a is not None or b is not None:
return a or cast(str, b)
return 'spam' assert: def f(a: str | None, b: str | None) -> str:
if a is not None or b is not None:
if a:
return a
assert b is not None
return b
return 'spam' third option: def f(a: str | None, b: str | None) -> str:
if a is not None or b is not None:
return a or b or 'impossible'
return 'spam' There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, not more ergonomic in this case, but better in the cases where I usually run into this. But maybe I dislike cast more than most people :-) I'd do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh oops, I guess you'd have to do |
||||||
slightly more verbose: | ||||||
|
||||||
.. code-block:: python | ||||||
|
||||||
def f(a: str | None, b: str | None) -> str: | ||||||
if a is not None: | ||||||
return a | ||||||
elif b is not None: | ||||||
return b | ||||||
return 'spam' | ||||||
|
||||||
|
||||||
issubclass | ||||||
~~~~~~~~~~ | ||||||
|
||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strangely we didn't mention this