Skip to content
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

Add SupportsIndex support to sequence formatters #234

Merged
merged 3 commits into from
Jan 13, 2022
Merged

Add SupportsIndex support to sequence formatters #234

merged 3 commits into from
Jan 13, 2022

Conversation

avylove
Copy link
Collaborator

@avylove avylove commented Jan 12, 2022

Mypy has been complaining about Sequence methods ljust(), rjust(), and center() which are part of str, but overwritten. It looks like these use the SupportsIndex type rather than int, which just means they have an __index__() method. Most of the time this means int, but a custom type could implement the method and then you could use it instead of an int.

>>> class Foo:
...     def __index__(self):
...         return 10
... 
>>> 'abcd'.rjust(Foo())
'      abcd'

This PR fixes those type annotations and adds parity support to the Sequence formatting methods (rjust(), ljust(), center(), truncate()), so they can also take a custom class that implements the __index__() method for width.

@codecov
Copy link

codecov bot commented Jan 12, 2022

Codecov Report

Merging #234 (14644ea) into master (1e53577) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #234   +/-   ##
=======================================
  Coverage   95.36%   95.36%           
=======================================
  Files           9        9           
  Lines        1014     1015    +1     
  Branches      176      176           
=======================================
+ Hits          967      968    +1     
  Misses         43       43           
  Partials        4        4           
Impacted Files Coverage Δ
blessed/sequences.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1e53577...14644ea. Read the comment docs.

@avylove avylove changed the title Use SupportsIndex type for width in str methods Add SupportsIndex support to sequence formatters Jan 12, 2022
Copy link
Owner

@jquast jquast left a comment

Choose a reason for hiding this comment

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

very cool, thanks, and happy new year!

@avylove avylove merged commit 165f43d into master Jan 13, 2022
@avylove avylove deleted the mypy_fix branch January 13, 2022 00:37
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.

None yet

2 participants