Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[mypyc] Add bytearray support - #10891

Merged
JukkaL merged 3 commits into
python:masterfrom
97littleleaf11:support-bytearray
Jul 30, 2021
Merged

[mypyc] Add bytearray support#10891
JukkaL merged 3 commits into
python:masterfrom
97littleleaf11:support-bytearray

Conversation

@97littleleaf11

@97littleleaf11 97littleleaf11 commented Jul 29, 2021

Copy link
Copy Markdown
Collaborator

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
assert brr3 == b'\x00\x01\x02\x03\x04'
brr4 = bytearray('string', 'utf-8')
assert brr4 == bytearray(b'string')
assert brr4 == b'string'

Copy link
Copy Markdown
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 .

@JukkaL JukkaL left a comment

Copy link
Copy Markdown
Collaborator

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