Skip to content

[mypyc] Add bytearray support #10891

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

Merged
merged 3 commits into from
Jul 30, 2021
Merged

Conversation

97littleleaf11
Copy link
Collaborator

@97littleleaf11 97littleleaf11 commented Jul 29, 2021

Description

bytearray is treated as a subtype of bytes by mypy, even though they behavior differently in some cases. We keep this design and check bytearray before calling corresponding cpython API.

Test Plan

Adds tests for bytearray creation.

@97littleleaf11 97littleleaf11 marked this pull request as ready for review July 29, 2021 14:45
brr4 = bytearray('string', 'utf-8')
assert brr4 == bytearray(b'string')
assert brr4 == b'string'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explicitly annotate the variables as bytes, since right now the inferred type will be bytearray, which doesn't have a mypyc primitive type, so it reverts back to object.

Also test calling a function that accepts bytes with an actual argument that is a bytearray object annotated with type Any, to check the runtime type checking from Any to bytes works if the runtime value is a bytearray .

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates! Looks good now.

@JukkaL JukkaL merged commit 2c90912 into python:master Jul 30, 2021
sthagen added a commit to sthagen/python-mypy that referenced this pull request Jul 30, 2021
[mypyc] Add bytearray support (python#10891)
@97littleleaf11 97littleleaf11 deleted the support-bytearray branch July 30, 2021 10:42
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.

2 participants