python-attrs / python-attrs/attrs

Support 3.14 annotations in converter functions

Open
#1,470 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

While #1451 added support for forward references in the classes themselves, forward references in converter functions still fail due to the use of inspect.signature which by default uses Format.VALUE when getting annotations.

Example:

from attrs import define, field

@define
class Works:
    x: unknown1

def converter(x: unknown2 | str) -> str:
    return str(x)

@define
class Fails:
    x: str = field(converter=converter)

Result:

NameError: name 'unknown2' is not defined

Cause:
https://github.com/python-attrs/attrs/blob/1315e42fe51743fe1c3529da6792dcda2ef6cc1f/src/attr/_compat.py#L50

For Python 3.14 annotations you now need:

self.sig = inspect.signature(callable, annotation_format=annotationlib.Format.FORWARDREF)

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.

Research direction

Start in src/attr/_compat.py at the inspect.signature call linked in the issue, then reproduce the Fails example with the converter annotation. Confirm that the converter accepts unresolved forward references under Python 3.14 without raising NameError; the issue does not name a specific test file.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.