python / python/mypy

Literal type math

Open
#11,990 11 comments 8 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature topic-literal-types
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

I am working on feature where reveal_type(1 + 2) will reveal Literal[3].

Types that I want to support:

  • int
  • str
  • bool

And these operations:

_SUPPORTED_LITERAL_OPERATIONS: Final = {
    int: ('+', '-', '*', '//'),  # `/` returns `float`
    str: ('+',),
    bool: ('and', 'or'),
}

So, True or False is revealed as Literal[True].

I will finish some test later today and send a PR in the morning.

Снимок экрана 2022-01-14 в 0 44 36

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 from the reveal_type examples and the proposed _SUPPORTED_LITERAL_OPERATIONS mapping. Trace how mypy evaluates expressions and represents literal types, then add coverage for the listed int, str, and bool operations. Done means the supported expressions reveal the expected Literal values while division remains float.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.