You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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()
```
0 commit comments