You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Overloaded signature with LiteralString is seen as "broader or same" as str #18584
(A clear and concise description of what the bug is.)
To Reproduce
importosfromtypingimportTYPE_CHECKING, overloadifTYPE_CHECKING:
from_typeshedimportStrPathfromtyping_extensionsimportLiteralString, reveal_type@overloaddefinsert_before_extension(
filename: LiteralString|os.PathLike[LiteralString], content: LiteralString
) ->LiteralString: ...
@overloaddefinsert_before_extension(filename: StrPath, content: str) ->str: ...
definsert_before_extension(filename: StrPath, content: str) ->str:
""" Given a filename and some content, insert the content just before the extension. >>> insert_before_extension('pages.pdf', '-old') 'pages-old.pdf' """parts=list(os.path.splitext(filename))
parts[1:1] = [content]
return''.join(parts)
a=insert_before_extension("", "")
reveal_type(a)
b=insert_before_extension(str(0), str(0))
reveal_type(b)
Expected Behavior
I expected mypy to see the overloads as being different so that a function passed literal inputs can propagate its output as also being literal (ie: not a user input). With a fallback to a regular str when the input isn't statically controlled.
Pyright supports this. Results from Pylance:
Type of "a" is "LiteralString"
Type of "b" is "str"
Actual Behavior Overloaded function signature 2 will never be matched: signature 1's parameter type(s) are the same or broader [overload-cannot-match]
In both cases: Revealed type is "builtins.str"
Your Environment
Mypy version used: mypy 1.14.1 (compiled: yes)
Mypy command-line flags: --strict
Mypy configuration options from mypy.ini (and other config files): None
This is working as expected because mypy does not support PEP 675 yet.
mypy currently treats LiteralString as an alias to str, meaning both overloads in your example are the same, hence the error.
Activity
hamdanal commentedon Feb 2, 2025
This is working as expected because mypy does not support PEP 675 yet.
mypy currently treats
LiteralStringas an alias tostr, meaning both overloads in your example are the same, hence the error.Duplicate of #12554