Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix cross-variable type-narrowing example (#17488)
From [<i>Type narrowing</i> § <i>Limitations</i>](https://github.com/python/mypy/blob/606971807fad1de26ebc575d327d4c1c33f71c0e/docs/source/type_narrowing.rst#limitations): ```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' ``` A trivial counter-example is `f('', None)`, which returns `None`. Ironically, this somewhat makes Mypy's diagnostic "correct". I propose that `str` be replaced with a custom class `C` whose `__bool__()` is not defined (does it have to be `@final` too?): ```python class C: pass def f(a: C | None, b: C | None) -> C: if a is not None or b is not None: return a or b # Incompatible return value type (got "C | None", expected "C") return C() ```
- Loading branch information