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

Skip to content

'%d' % SupportsInt fails #7114

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

Closed
jolaf opened this issue Jul 1, 2019 · 4 comments · Fixed by #7418
Closed

'%d' % SupportsInt fails #7114

jolaf opened this issue Jul 1, 2019 · 4 comments · Fixed by #7418
Labels

Comments

@jolaf
Copy link
Contributor

jolaf commented Jul 1, 2019

i: SupportsInt = 5
s = '%d' % i

results in

Incompatible types in string interpolation (expression has type "SupportsInt", placeholder has type "Union[int, float]")

While in runtime it provides a reasonable '5'.

@msullivan
Copy link
Collaborator

This should be a pretty straightforward fix to checkstrformat if you (or someone else) wants to submit a PR

@Patil2099
Copy link

@msullivan Where do I have to add it? I can make a PR!

@JukkaL
Copy link
Collaborator

JukkaL commented Aug 29, 2019

It looks like fixing this involves modifying the conversion_type function in mypy/checkstrformat.py and writing a test/tests for this.

@msullivan
Copy link
Collaborator

@Patil2099 it looks like Ivan has fixed this as part of a broader overhaul of string formatting, though. Sorry!

ilevkivskyi added a commit that referenced this issue Sep 24, 2019
Fixes #1444
Fixes #2639
Fixes #7114

This PR does three related things:
* Fixes some corner cases in `%` formatting
* Tightens `bytes` vs `str` interactions (including those that are technically not errors)
* Adds type checking for `str.format()` calls

This also fixes few issues discovered by this PR during testing (notably a bug in f-string transformation), and adds/extends a bunch of docstring and comments to existing code. The implementation is mostly straightforward, there are few hacky things, but IMO nothing unreasonable.

Here are few comments:
* It was hard to keep the approach to `str.format()` calls purely regexp-based (as for `%` formatting), mostly because the former can be both nested and repeated (And we must support nested formatting because this is how we support f-strings). CPython itself uses a custom parser but it is huge, so I decided to have a mixed approach. This way we can keep code simple while still maintaining practically one-to-one compatibility with runtime behavior (the error messages are sometimes different however).
* This causes few hundreds of errors internally, I am not sure what to do with these. Most popular ones are:
  - Unicode upcast (`'%s' % u'...'` results in `unicode`, not `str`, on Python 2)
  - Using `'%s' % some_bytes` and/or `'{:s}'.format(some_bytes)` on Python 3
  - Unused arguments in `str.format()` call (probably because at runtime they are silently ignored)
* I added new error code for string interpolation/formatting and used it everywhere in old and new code. Potentially we might split out the `str` vs `bytes` errors into a separate error code, because technically they are not type errors, just suspicious/dangerous code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants