Skip to content

3.12+: compile() and some wrappers of it don't accept non-bytes buffers for filename argument at runtime #13881

Open
@Sachaa-Thanasius

Description

@Sachaa-Thanasius

The underlying C API that compile() uses to convert/validate its filename argument, PyUnicode_FSConverter, doesn't accept non-bytes buffers anymore (see python/cpython#98393), but typeshed still has that parameter typed with (collections.abc|typing_extensions).Buffer instead of bytes. Having a 3.12+ version if branch for the updated function definition would make sense, imo.

Min. Repro. Example

(.venv) thanos@DESKTOP-DERJ7DF:~$ python3.11 --version
Python 3.11.11
(.venv) thanos@DESKTOP-DERJ7DF:~$ python3.11 -c "compile('a=1', bytearray(b'<bytes>'), 'exec')"
<string>:1: DeprecationWarning: path should be string, bytes, or os.PathLike, not bytearray
(.venv) thanos@DESKTOP-DERJ7DF:~$ python3.12 --version
Python 3.12.9
(.venv) thanos@DESKTOP-DERJ7DF:~$ python3.12 -c "compile('a=1', bytearray(b'<bytes>'), 'exec')"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: expected str, bytes or os.PathLike object, not bytearray

At the moment, I can think of a few stdlib functions that pass a filename argument through to compile() and thus also need to be updated:

  • ast.parse()
  • importlib.machinery.SourceFileLoader.source_to_code() (maybe also the corresponding methods in its base classes?)

Not sure what else is affected by this deprecation-turned-error, but there might be more.

Metadata

Metadata

Assignees

No one assigned

    Labels

    stubs: false negativeType checkers do not report an error, but should

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions