python / python/cpython

tarfile.TarFile.errors is initialized for some reason as None even though it really only accepts str

Open
#127,424 3 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

Bug description:

See the exact line here: https://github.com/python/cpython/blob/ee57221872ad009df648e22d2f113f907607ec16/Lib/tarfile.py#L1687

After that the errors is used only as string in encode/decode. And is initialized in the dunder init with default errors="surrogateescape".

So perhaps it would be a good idea to use errors="" in #L1687 ? or directly with the default errors="surrogateescape" ?

This was discovered as part of the typeshed, where tarfile.TarFile.errors was added into the allowed list since mypy complained: https://github.com/python/typeshed/blob/2ccc53bb6716463680fcf1d204642c46299b7f88/stdlib/%40tests/stubtest_allowlists/common.txt#L46C1-L46C16

I will add a PR here and to the typeshed if this gets approved.

CPython versions tested on:

3.13

Operating systems tested on:

Windows

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 Lib/tarfile.py line 1687 and inspect the TarFile initializer and the later encode/decode uses of errors. Check the related typeshed allowlist entry, then confirm the runtime initialization and typing agree; update the corresponding CPython and typeshed changes if needed.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.