KotlinIsland / KotlinIsland/basedmypy

value with `Any` in union type does not narrow when assigned to variable with non-`Any` type

Open
#608 3 comments 0 reactions 0 assignees View on GitHub
bug p-2 topic-any
Dominant language
Python
Stars
202
Forks
6
PR merge metrics
No merged PRs in 30d

Description

this does not happen in upstreamed mypy

- [basedmypy playground](https://mypy-play.net/?mypy=basedmypy-2.3.0&python=3.12&gist=3dd058a366c3237bcfadcdb551c4ce9e)
- [mypy playground](https://mypy-play.net/?mypy=latest&python=3.12&flags=show-error-context%2Cshow-error-codes%2Cstrict%2Ccheck-untyped-defs%2Cdisallow-any-decorated%2Cdisallow-any-expr%2Cdisallow-incomplete-defs%2Cdisallow-subclassing-any%2Cdisallow-untyped-calls%2Cdisallow-untyped-decorators%2Cdisallow-untyped-defs%2Cno-implicit-reexport%2Clocal-partial-types%2Cstrict-equality%2Cwarn-incomplete-stub%2Cwarn-redundant-casts%2Cwarn-return-any%2Cwarn-unreachable%2Cwarn-unused-configs%2Cwarn-unused-ignores%2Cno-implicit-optional&gist=57026a46f2e0942012976ff9d5c3bb70)

### Gist to reproduce

```python
# mypy: allow-any-explicit=true

from typing import Any, cast

def any() -> Any: ...

foo: str = any() or "" # type:ignore[no-any-expr]

reveal_type(foo) # error: Expression type contains "Any" (has type "Any | str")

bar: str = any()

reveal_type(bar) # no error, revealed type is "str"
```

Contributor guide

Open the contributing guide

Research direction

Run the provided snippet in the basedmypy and upstream mypy playground links to compare their diagnostics for the two assignments and reveal_type calls. Trace the type-narrowing path responsible for `Any | str` in the `or` expression; done means the reproduced case narrows `foo` consistently with the direct `bar` assignment without introducing unrelated diagnostics.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.