Skip to content

Comments

Fix #13881: Add plugin to disallow bytearray as filename in compile()#14173

Open
vidhyavijayan3 wants to merge 3 commits intopython:mainfrom
vidhyavijayan3:13881-fix-compile-filename-typing
Open

Fix #13881: Add plugin to disallow bytearray as filename in compile()#14173
vidhyavijayan3 wants to merge 3 commits intopython:mainfrom
vidhyavijayan3:13881-fix-compile-filename-typing

Conversation

@vidhyavijayan3
Copy link
Contributor

In Python 3.12 and later, passing a bytearray as the filename argument to
compile() is no longer allowed. This commit adds a mypy plugin that checks
for this specific misuse and emits an appropriate error message.

The plugin is applied conditionally via mypy.ini and is compatible with the
existing stubs for compile() in builtins.pyi.

This prevents false positives and ensures correct typing behavior in
accordance with the updated CPython semantics.

Closes #13881

@srittau
Copy link
Collaborator

srittau commented May 28, 2025

Please note that typeshed only provides type annotations, we don't need or want any custom mypy plugins – that would be detrimental to typeshed's purpose. To solve #13881, we just need to change the type annotations for compile's filename argument. Probably to StrPath | bytes, but I haven't looked too deeply into it. Probably StrOrBytesPath.

@vidhyavijayan3
Copy link
Contributor Author

Thank you for the clarification! I'll remove the custom mypy plugin and instead update the type annotation for the filename parameter in the compile() stub to use StrOrBytesPath, as suggested. Appreciate your feedback and guidance on aligning with typeshed's goals.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants