Can mypy track string literals?

2024/9/8 10:53:11

Is there anyway to make this work

from typing import Literal
def foo(bar: Literal["bar"]) -> Literal["foo"]:foo = "foo"return foobar = "bar"
foo(bar)

Here are the errors

foo.py:4: error: Incompatible return value type (got "str", expected "Literal['foo']")
foo.py:8: error: Argument 1 to "foo" has incompatible type "str"; expected "Literal['bar']"

Is pretty obvious that foo variable and bar are literals because they are assigned to literals, so this is safe, but mypy seems to not track this. Is there anything that I'm missing?

Answer

MyPy infers literals to their builtin type, not a Literal of their value.

mypy Docs » Literal types

You must explicitly add an annotation to a variable to declare that it has a literal type. [..] variables without this annotation are not assumed to be literals.

To allow inference of Literal values, annotate the variable as Final:

from typing import Finalfrom typing_extensions import Finalbar: Final = "bar"
reveal_type(bar)  # Revealed type is 'Literal['bar']?'

Annotating a variable as Final indicates that its value will not be substituted for a value of similar type. This makes it correct to infer the type as the specific Literal value, instead of just the general type.

Note that this inference is contextual: The type is inferred as Literal for all cases where a Literal is expected. For cases in which a type is expected, be it the literal type, a base type or TypeVar, the type is inferred as the general type.

reveal_type([bar])  # Revealed type is 'builtins.list[builtins.str*]'
https://en.xdnf.cn/q/72594.html

Related Q&A

Lazy loading of attributes

How would you implement lazy load of object attributes, i.e. if attributes are accessed but dont exist yet, some object method is called which is supposed to load these?My first attempt isdef lazyload…

Using pythons property while loading old objects

I have a rather large project, including a class Foo which recently needed to be updated using the @property decorator to create custom getter and setter methods.I also stored several instances of Foo …

How to replace all those Special Characters with white spaces in python?

How to replace all those special characters with white spaces in python ?I have a list of names of a company . . . Ex:-[myfiles.txt] MY company.INCOld Wine pvtmaster-minds ltd"apex-labs ltd"…

Python 3.x : move to next line

Ive got a small script that is extracting some text from a .html file.f = open(local_file,"r") for line in f:searchphrase = <span class="positionif searchphrase in line:print("fo…

Why couldnt Julia superset python?

The Julia Language syntax looks very similar to python, while the concept of a class (if one should address it as such a thing) is more what you use in C. There were many reasons why the creators decid…

How to log into Google Cloud Storage from a python function?

I am new to google cloud storage and I try to set up a function that downloads a blob once a day. At the moment I am working in my Jupyter Notebook but finally, the code will run in an Azure Function. …

OpenCV Python, reading video from named pipe

I am trying to achieve results as shown on the video (Method 3 using netcat)https://www.youtube.com/watch?v=sYGdge3T30oThe point is to stream video from raspberry pi to ubuntu PC and process it using …

Load model with ML.NET saved with keras

I have a Neural Network implemented in Python with Keras. Once I have trained it I have exported the model and I have got two files: model.js and model.h5. Now I want to classify in real time inside a …

unable to add spark to PYTHONPATH

I am struggling to add spark to my python path:(myenv)me@me /home/me$ set SPARK_HOME="/home/me/spark-1.2.1-bin-hadoop2.4" (myenv)me@me /home/me$ set PYTHONPATH=$PYTHONPATH:$SPARK_HOME:$SPARK_…

Is there a way to shallow copy an existing file-object?

The use case for this would be creating multiple generators based on some file-object without any of them trampling each others read state. Originally I (thought I) had a working implementation using s…