jquast / blessed

Blessed is an easy, practical library for making python terminal apps
http://pypi.python.org/pypi/blessed
MIT License
1.2k stars 72 forks source link

Add SupportsIndex support to sequence formatters #234

Closed avylove closed 2 years ago

avylove commented 2 years ago

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[bot] commented 2 years ago

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.