Skip to content

Fix %c string and bytes interpolation #10869

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 2 commits into from
Jul 25, 2021

Conversation

97littleleaf11
Copy link
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