python / python/cpython

annotationlib.type_repr() returns "None.list.append" for bound built-in methods

Open
#152,692 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

annotationlib.type_repr() (a public, documented helper, exported in
__all__) returns a string that names a nonexistent module None for bound
built-in methods:

>>> from annotationlib import type_repr
>>> type_repr([].append)
'None.list.append'
>>> type_repr(dict.fromkeys)
'None.dict.fromkeys'
>>> import random; type_repr(random.random)
'None.Random.random'

Bound built-in methods (and C-accelerator functions) are BuiltinFunctionType
with __module__ set to None, so f"{value.__module__}.{value.__qualname__}"
interpolates the literal None. Every other object in this family produces a
resolvable name -- len -> 'len', os.getpid -> 'posix.getpid';
this is the only one that emits a None. prefix. 'None.list.append' is
also an active hazard: a STRING-format consumer that resolves it gets
AttributeError on the literal None.

annotations_to_string() and get_annotations(obj, format=Format.STRING)
propagate it when an annotation value is such a method.

Note for completeness: this is reached when a live method object is passed to
type_repr (its documented purpose), not from natural annotation source
syntax -- def f(x: [].append) is stringified correctly via the AST path.
The defect is the public-API output of type_repr itself.

Fix

Return __qualname__ ('list.append') when __module__ is None,
exactly as already done for the "builtins" module. repr() is not a
usable fallback: it embeds a non-deterministic heap address and is not
re-parseable.

Linked PRs
  • gh-152693

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 at the public annotationlib.type_repr() entry point and inspect how it formats objects whose module is None. Verify the documented examples and the annotations_to_string() and get_annotations(..., format=Format.STRING) paths; done means bound built-in methods produce their qualname without a None. prefix and remain resolvable.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.