python / python/typeshed

colorful how to handle dynamic types

Open
#15,012 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stubs: false positive
Dominant language
Python
Stars
5.1k
Forks
2.1k
Avg merge
1d 19h
Merged PRs (30d)
82

Description

I played around with the new colorful types and there is a interesting problem.
Colorful takes full advantage of __getattr__ and im a bit out of ideas how to type this.

Common usage is (also seen in the readme):

import colorful as cf
print(cf.bold_white('Hello World'))

Where bold_white is dynamically build with the dynamically loadable color plate and a modifier.

Which makes this valid working python code:

import colorful as cf
print(cf.bold_underlined_struckthrough_italic_bold_red("typeshed"))

where the color from the pallet is red and the modifiers are separated by a _ and need to be out of a list of MODIFIERS but otherwise can be combined in any order and even be repeated.

MODIFIERS = {
    'reset': (0, 0),
    'bold': (1, 22),
    'dimmed': (2, 22),
    'italic': (3, 23),
    'underlined': (4, 24),
    'blinkslow': (5, 25),
    'blinkrapid': (6, 25),
    'inversed': (7, 27),
    'concealed': (8, 28),
    'struckthrough': (9, 29)
}

Is there any good solution to type something this dynamic?
Or is the only way to kinda allow everything after colorful.<anything>

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with colorful's README usage and the dynamic getattr behavior described in the issue, then examine how the MODIFIERS combinations are formed. Done would require an agreed typing approach for dynamically constructed attributes; the issue names no target file or test.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
developer-experience
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.