KotlinIsland / basedmypy

Based Python static type checker with baseline, sane default settings and based typing features
https://kotlinisland.github.io/basedmypy/
Other
145 stars 4 forks source link

Recursive sequence type of `Sequence[NotAString]` should not accept `str` #819

Open jorenham opened 1 day ago

jorenham commented 1 day ago

This is a (p)repost from https://github.com/python/mypy/issues/18184, which also applies here:

The false negative can be reproduced with:

from collections.abc import Sequence
from typing import TypeAlias

class Scalar: ...

Vector: TypeAlias = Sequence[Scalar]
Tensor: TypeAlias = Vector | Sequence["Tensor"]

accepted_vector: Vector = [Scalar()]     # true negative
rejected_vector: Vector = "duck"         # true positive: ducks aren't vectors

accepted_tensor: Tensor = [[[Scalar()]]] # true negative
rejected_tensor1: Tensor = object()      # true positive: objects aren't tensors
rejected_tensor2: Tensor = "duck"        # false negative: ducks are tensors...?

mypy-play

jorenham commented 1 day ago

oh woops, this should be labeled as bug

KotlinIsland commented 21 hours ago

oh woops, this should be labeled as bug

feel free to update it 😁

KotlinIsland commented 20 hours ago

is this the same thing?

class C[T]: ...
type A = C[A] | int
type B = C[B]

b: B
a: A = b
KotlinIsland commented 20 hours ago
jorenham commented 19 hours ago
class C[T]: ...
type A = C[A] | int
type B = C[B]

b: B
a: A = b

If T is covariant, and mypy treats str like a recursive type, then it might indeed be a more general example.


edit it's actually a different, but similar, issue. Because this Sequence issue actually works in as expected pyright, but your A, B example is accepted in both mypy and pyright:

jorenham commented 19 hours ago

is this the same thing?

class C[T]: ...
type A = C[A] | int
type B = C[B]

b: B
a: A = b

What's the | int for btw 🤔 ?

KotlinIsland commented 18 hours ago

What's the | int for btw 🤔 ?

just for being closer to the original egg

jorenham commented 18 hours ago

this seems relevant

https://github.com/KotlinIsland/basedmypy/blob/0935784ab77e91c869bae67be01ba0f420678620/mypy/subtypes.py#L170-L189