Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --strict-bytes flag to disable promotions to bytes #18256

Closed
JukkaL opened this issue Dec 6, 2024 · 1 comment · Fixed by #18263
Closed

Add --strict-bytes flag to disable promotions to bytes #18256

JukkaL opened this issue Dec 6, 2024 · 1 comment · Fixed by #18263

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Dec 6, 2024

Feature

We currently have the undocumented flags --disable-bytearray-promotion and --disable-memoryview-promotion that turn off promotions from bytearray/memoryview to bytes. These are a bit unwieldy, and I'm proposing that we add a new flag called --strict-bytes that enables both of these flags, and make it an officially documented feature. In mypy 2.0 we'd switch --strict-bytes on by default.

Additional details:

  • This should be documented.
  • Also support --no-strict-bytes for the inverse. This allows users to prepare for mypy 2.0 even if they aren't ready to turn on the flag.
  • This probably shouldn't be a per-module option, since migration to the new behavior appears to be fairly easy even for large codebases.
  • We'll still keep the old undocumented flags for now, but they might be removed in the future.

Pitch

Currently bytearray and memoryview are assignable to bytes, which is incorrect, but this has been tricky to fix mostly due to stub quality. Stubs seem to have improved enough for this to be practical now -- any anyway switching the default would give more incentives to fix stubs.

The new flag is shorter and easier to remember, and it combines both of the features so it's simpler. The naming is also arguably consistent with --strict-equality and friends. In my experience dropping the memoryview promotion rarely causes trouble, so having it as a separate flag doesn't seem important.

@JukkaL
Copy link
Collaborator Author

JukkaL commented Dec 6, 2024

Anybody want to work on this? We'd probably need a feature release with this available before we can release mypy 2.0, due to our backward compatibility policy.

hamdanal added a commit to hamdanal/mypy that referenced this issue Dec 7, 2024
@JukkaL JukkaL closed this as completed in 6427ef1 Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant