Skip to content

Improve typing #44

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 5 commits into from
Oct 14, 2023
Merged

Improve typing #44

merged 5 commits into from
Oct 14, 2023

Conversation

marcin-serwin
Copy link
Contributor

This PR introduces two changes to the declared type signatures:

  1. The allowed colors, highlights, and attributes are now checked against defined string literals. This means that, e.g., cprint("hello", "orange") will result in
    error: Argument 2 to "cprint" has incompatible type "Literal['orange']"; expected "Optional[Literal['black', 'grey', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'light_grey', 'dark_grey', 'light_red', 'light_green', 'light_yellow', 'light_blue', 'light_magenta', 'light_cyan', 'white']]"
    
    when using type checker.
  2. The type of first argument of colored and cprint was relaxed to accept objects and not just strings. This makes cprint more in line with print, which accepts arbitrary objects and fixes the example in README which reads
    for i in range(10):
        cprint(i, "magenta", end=" ")
    which currently results in error: Argument 1 to "cprint" has incompatible type "int"; expected "str" when running through mypy.

@codecov
Copy link

codecov bot commented Apr 18, 2023

Codecov Report

All modified lines are covered by tests ✅

Comparison is base (2ebb782) 100.00% compared to head (7b7d1a6) 100.00%.

Additional details and impacted files
@@            Coverage Diff            @@
##              main       #44   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            3         4    +1     
  Lines          116       125    +9     
=========================================
+ Hits           116       125    +9     
Flag Coverage Δ
macos-latest 100.00% <100.00%> (ø)
ubuntu-latest 100.00% <100.00%> (ø)
windows-latest 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
src/termcolor/_types.py 100.00% <100.00%> (ø)
src/termcolor/termcolor.py 100.00% <100.00%> (ø)
tests/test_termcolor.py 100.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@hugovk hugovk left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! Here's a couple of suggestions.

@vidhanio
Copy link

vidhanio commented Oct 3, 2023

any way this could be merged? this seems like an amazing upgrade for usability, and the only blocker seems to be impossible to do in the current state of python typing.

@hugovk
Copy link
Member

hugovk commented Oct 13, 2023

Thanks for ping. If the conflicts are resolved, I'll take another look.

@hugovk hugovk enabled auto-merge (squash) October 14, 2023 06:29
@hugovk hugovk merged commit fd58567 into termcolor:main Oct 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog: Changed For changes in existing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants