For the following method: [code] Why do I get the following warning if I can assure that…
- User
For the following method:
def _load_model(self) -> Model: model: Model = {"vertices": [], "texture_coord": [], "faces": []} material: str | None = None for line in open(f"src/objects/{self._name}/model.obj", "r"): if line.startswith("#"): continue values = line.split() match values[0]: case "v": # recovering vertices model["vertices"].append( tuple([float(x) for x in values[1:4]]) ) case "vt": # recovering texture coordinates model["texture_coord"].append( tuple([float(x) for x in values[1:3]]) ) case "f": face: list[int] = [] face_texture: list[int] = [] for v in values[1:]: w: list[str] = v.split("/") face.append(int(w[0])) if len(w) >= 2 and len(w[1]) > 0: face_texture.append(int(w[1])) else: face_texture.append(0) model["faces"].append((face, face_texture, material)) case "usemtl" | "usemat": material = values[1] case _: pass return modelWhy do I get the following warning if I can assure that the list will have the precise amount of elements?
│ └╴ object.py 2 │ ├╴ Argument of type "tuple[float, ...]" cannot be assigned to parameter "object" of type "tuple[float, float, float]" in function "append" │ │ "tuple[float, ...]" is not assignable to "tuple[float, float, float]" │ │ Tuple size mismatch; expected 3 but received indeterminate basedpyright (reportArgumentType) [142, 25] │ └╴ Argument of type "tuple[float, ...]" cannot be assigned to parameter "object" of type "tuple[float, float]" in function "append" │ "tuple[float, ...]" is not assignable to "tuple[float, float]" │ Tuple size mismatch; expected 2 but received indeterminate basedpyright (reportArgumentType) [146, 25] - Assistant
The warning you are seeing comes from type checking (likely from
mypyor Pyright) and is related to how you are constructing tuples with a list comprehension and then passing them toappend().The core issue:
tuple([float(x) for x in values[1:4]])Here, the type checker infers the type as
tuple[float, ...](a tuple of floats of unknown length), because the list comprehension produces aList[float]of variable length, and thentuple()converts it to a tuple of unknown length.However, your
modeldictionary is typed as:model: Model = {"vertices": [], "texture_coord": [], "faces": []}and presumably,
Modelis defined such that:verticesis a list oftuple[float, float, float]texture_coordis a list oftuple[float, float]
The type checker expects fixed-length tuples of exactly 3 or 2 floats, but it sees a tuple of variable length (
tuple[float, ...]), so it complains about a size mismatch.
Why does this happen?
tuple([float(x) for x in values[1:4]])creates a tuple from a list, but the type checker cannot guarantee the length is exactly 3 at type-check time.- The slice
values[1:4]should always have length 3, but the type checker does not analyze the slice length precisely. - So it treats the result as
tuple[float, ...](variable length tuple), which is incompatible withtuple[float, float, float].
How to fix it?
You need to help the type checker understand the fixed length of the tuple. Here are some ways:
1. Use explicit tuple construction
Instead of a list comprehension, explicitly construct the tuple:
model["vertices"].append(( float(values[1]), float(values[2]), float(values[3]) ))Similarly for texture coordinates:
model["texture_coord"].append(( float(values[1]), float(values[2]) ))This way, the type checker knows the tuple length is exactly 3 or 2.
2. Use a cast (less recommended)
If you want to keep the list comprehension, you can cast:
from typing import cast model["vertices"].append( cast(tuple[float, float, float], tuple(float(x) for x in values[1:4])) )But this is less clean and bypasses type safety.
3. Define helper functions
You can define a helper function that returns a fixed-length tuple:
from typing import Tuple def to_vec3(values: list[str]) -> Tuple[float, float, float]: return (float(values[0]), float(values[1]), float(values[2])) def to_vec2(values: list[str]) -> Tuple[float, float]: return (float(values[0]), float(values[1])) # Then use: model["vertices"].append(to_vec3(values[1:4])) model["texture_coord"].append(to_vec2(values[1:3]))
Summary
The warning is because the type checker cannot verify the tuple length when you create it from a list comprehension and then convert to a tuple. To fix it, explicitly construct the tuple with fixed length, so the type checker can confirm the exact tuple size.
If you want, I can help you rewrite the method with these fixes!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.