Pylance doesn't recognise attributes obtained from `fields`

Open
#1,361 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Quiet
Tech stack
python
Domain
tooling

Research direction

The issue provides a Python MRE but names no repository files or tests. Start by reproducing the behavior with attrs and Pylance, then inspect how the generated AAttributes type is exposed; done means determining whether attrs can provide the needed typing information or whether this belongs in Pylance.

Written by the indexing model from the issue text.

Description

Pyright Typing

Consider this MRE

from attrs import define, fields

@define
class A:
    foo: int
    bar: float

f = fields(A)
print(f.foo)
Attribute(name='foo', default=NOTHING, validator=None, repr=True, eq=True, eq_key=None, order=True, order_key=None, hash=None, init=True, metadata=mappingproxy({}), type=<class 'int'>, converter=None, kw_only=False, inherited=False, on_setattr=None, alias='foo')

This all works fine.

The only thing that isn't working perfectly is Pylance doesn't seem to properly recognise this. It highlights the attribute in white and incorrect hover text is provided.
Untitled

Looking at the types, I found

print(type(f))
<class 'AAttributes'>

But I couldn't find any reference to AAttributes in the codebase.

My question is, is there something that attrs needs to do to make Pylance work here? Or is this a bug/limitation with Pylance and I should open a ticket with them to get support for this?

Thanks.

Dominant language
Python
Stars
5.8k
Forks
480
Avg merge
2h 15m
Merged PRs (30d)
2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python-attrs/attrs

All issues in python-attrs/attrs

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.