Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try tweaking attrs.fields() #15396

Closed
wants to merge 2 commits into from

Conversation

Tinche
Copy link
Contributor

@Tinche Tinche commented Jun 8, 2023

Trying to fix #15393 in an alternative way.

@JukkaL I might need some help here. I'm still getting builtins.object while iterating:

from attrs import define, fields


@define
class Test:
    a: int
    b: float


for f in fields(Test):
    reveal_type(f)  # note: Revealed type is "builtins.object"

But if I do this:

for f in iter(fields(Test)):
    reveal_type(f)  # note: Revealed type is "attr.Attribute[Any]"

Any ideas?

@github-actions
Copy link
Contributor

github-actions bot commented Jun 8, 2023

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@ikonst
Copy link
Contributor

ikonst commented Jun 8, 2023

I might not be helping here, but what you did with copying the __iter__ symbol is a likely the outlier.

The normal behavior e.g. for a tuple, is:

t: tuple[list[int], list[float]]
for v in t:
    reveal_type(v)  # note: Revealed type is "builtins.object"
for v in iter(t):
    reveal_type(v)  # note: Revealed type is "builtins.object"
reveal_type(x.__iter__())  # note: Revealed type is "typing.Iterator[builtins.object]"

Update: This is the culprit. Tuples are special-cased.

@jhance
Copy link
Collaborator

jhance commented Jun 10, 2023

Can we add a new field to TupleType that is only for that callsite? Like joined_type_for_iter or something?

@Tinche
Copy link
Contributor Author

Tinche commented Jul 29, 2023

Superseded by #15688

@Tinche Tinche closed this Jul 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants