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

Export types from library for type checking #62

Open
charpercyr opened this issue Dec 1, 2023 · 4 comments
Open

Export types from library for type checking #62

charpercyr opened this issue Dec 1, 2023 · 4 comments

Comments

@charpercyr
Copy link

I am writing a logger with termcolor, and want to use Attribute, Color & Highlight from termcolor._types.
I am using mypy with aggressive type-checking so it will not type check unless I import the types (or ignore the error).

class ColorFormatter:
    FORMATS: ClassVar[dict[int, tuple[Color, list[Attribute] | None]]] = {
        logging.DEBUG: ("grey", None),
        logging.INFO: ("white", None),
        logging.WARNING: ("yellow", ["bold"]),
        logging.ERROR: ("red", ["bold"]),
        logging.CRITICAL: ("magenta", ["underline", "bold"]),
    }

Is it possible to export the types from __init__.py?

@hauntsaninja
Copy link

hauntsaninja commented Jan 2, 2024

It's hard to use termcolor 2.4 in statically typed codebases without this. Personally, I think it might be nice to not use literal types here at all, not sure it's worth the pain.

@Josef-Friedrich
Copy link

At the end I copied the literal types. Is there a better solution?

Color = Literal[
    "black",
    # ...
    "white",
]

Highlight = Literal[
    "on_black",
    # ...
    "on_white",
]


def color(
    text: str, color: Optional[Color] = None, on_color: Optional[Highlight] = None
) -> str:
    """Wrapper function around ``termcolor.colored()`` to easily turn off and
    on colorized terminal output on the command line.
    """
    settings = get_args()
    if settings.general_colorize:
        return termcolor.colored(text, color, on_color)
    else:
        return text

@vors
Copy link

vors commented Jan 2, 2024

It's hard to use termcolor 2.4 in statically typed codebases without this.

+1

@hugovk
Copy link
Member

hugovk commented Jan 4, 2024

Maybe we should remove/replace these literal types?

@hauntsaninja Would you like to propose something in a PR?

(Ref to where they were added: #44)

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

No branches or pull requests

5 participants