nutti / fake-bpy-module

Fake Blender Python API module collection for the code completion.
MIT License
1.42k stars 97 forks source link

`Vector` - cannot convert to tuple / list / use slices #274

Closed Andrej730 closed 4 months ago

Andrej730 commented 5 months ago

See example below - typing errors converting Vector to tuple/list/use slices though it is actually possible.

from mathutils import Vector
from typing import assert_type

v = Vector()
# Argument of type "Vector" cannot be assigned to parameter "iterable" of type "Iterable[_T_co@tuple]" in function "__new__"
#   "Vector" is incompatible with protocol "Iterable[_T_co@tuple]"
#     "__iter__" is not present
l = list(v)
assert_type(list(v), list[float])
t = tuple(v)
assert_type(t, tuple[float, ...])

# Argument of type "slice" cannot be assigned to parameter "key" of type "int" in function "__getitem__"
#   "slice" is incompatible with "int"
sl = v[:3]
assert_type(sl, tuple[float, ...])

sl2 = v[:2]
# Ideally this also should work since Vector size is always >= 2
# but not sure if it's possible.
assert_type(sl2, tuple[float, float])
Road-hog123 commented 4 months ago
  1. This is working correctly—Vector has no __iter__ method, so to a PEP484 type-checker it is not an Iterable even though it is iterable. We could add a fake __iter__ method, although ideally someone that knows C++ would add it for real.

  2. 278

  3. It's not possible—it would be up to the type checker to infer a more specific type from information outside the type system.

Andrej730 commented 3 months ago
  1. This is working correctly—Vector has no __iter__ method, so to a PEP484 type-checker it is not an Iterable even though it is iterable. We could add a fake __iter__ method, although ideally someone that knows C++ would add it for real.

@Road-hog123 @nutti Can we solve it for now by fake-implementing __iter__? Since there are cases when you want to convert Vector to native Python types like tuple or list and current typing shows false-positives and gets in the way.

nutti commented 3 months ago

@Andrej730

Yes, you can make a PR if you want.