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

Skip to content

Fix %c string and bytes interpolation - #10869

Merged
ilevkivskyi merged 2 commits into
python:masterfrom
97littleleaf11:mypy-bytes-support
Jul 25, 2021
Merged

Fix %c string and bytes interpolation#10869
ilevkivskyi merged 2 commits into
python:masterfrom
97littleleaf11:mypy-bytes-support

Conversation

@97littleleaf11

Copy link
Copy Markdown
Collaborator

Description

According to PEP461, conversion type %c performs differently for string and bytes. In addition, b'%c’ % str is not supported in Python3.

Test Plan

CPython Python3:

ss = 'a'
bb = b'a'
ii = 97
ff = 97.0

print('%c' % ss)
# print('%c' % '') # TypeError: %c requires int or char
# print('%c' % 'aa') # TypeError: %c requires int or char
# print('%c' % bb) # TypeError: %c requires int or char
print('%c' % ii)
# print('%c' % ff) # TypeError: %c requires int or char

# print(b'%c' % ss) # TypeError: %c requires an integer in range(256) or a single byte
# print(b'%c' % '') # TypeError: %c requires an integer in range(256) or a single byte
# print(b'%c' % 'aa') # TypeError: %c requires an integer in range(256) or a single byte
print(b'%c' % bb)
print(b'%c' % ii)
# print(b'%c' % ff) # TypeError: %c requires an integer in range(256) or a single byte

CPython Python2:

print('%c' % ss)
# print('%c' % '') # TypeError: %c requires int or char
# print('%c' % 'aa') # TypeError: %c requires int or char
print('%c' % bb)
print('%c' % ii)
# print('%c' % ff) # TypeError: integer argument expected, got float

print(b'%c' % ss)
# print('%c' % '') # TypeError: %c requires int or char
# print('%c' % 'aa') # TypeError: %c requires int or char
print(b'%c' % bb)
print(b'%c' % ii)
# print(b'%c' % ff) # TypeError: integer argument expected, got float

@ilevkivskyi
ilevkivskyi merged commit b3b3242 into python:master Jul 25, 2021
JukkaL pushed a commit that referenced this pull request Jul 26, 2021
This PR is a follow-up to #10869.

The error msg of mypy now is basically the same as CPython. We offer hints for 
incompatible types.
@97littleleaf11
97littleleaf11 deleted the mypy-bytes-support branch July 26, 2021 18:46
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