Skip to content

Consider unsafely assuming in narrowing that multiple inheritance won't happen #1578

@RandomBrainCode

Description

@RandomBrainCode

Summary

Is it possible for Ty to infer the type of the Iterator Variable when Iterator is properly defined?

In the below example running, area is properly defined above

		if allowed:
			area: list[Area] = await user.get_allowed_areas()
		else:
			area: Area = await user.area_get()

but when I run ty check, I get the following

error[unresolved-attribute]: Object of type `object` has no attribute `get_model`
   --> app/routers/me.py:113:36
    |
112 |     if isinstance(area, list):
113 |         models: list[AreaModel] = [await a.get_model() for a in area]
    |                                          ^^^^^^^^^^^
114 |         return JSONResponse(content=loads(dumps([a.content() for a in models])), status_code=200)
    |
info: rule `unresolved-attribute` is enabled by default

I was able to resolve it by adding a: Area above line 113, but that causes ruff to throw warning Local variable 'a' is annotated but never used

One thing, I am not sure if this could be impacting it, but the variable area can actually be of type Area or type list[Area], hence the if isinstance(area, list): at line 112.

Version

ty 0.0.1-alpha.26 (b225fd8 2025-11-10)

Metadata

Metadata

Assignees

No one assigned

    Labels

    narrowingrelated to flow-sensitive type narrowingneeds-decisionAwaiting a decision from a maintainer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions