Skip to content

Overloaded signature with LiteralString is seen as "broader or same" as str #18584

Closed as duplicate

Description

@Avasam

Bug Report

(A clear and concise description of what the bug is.)

To Reproduce

import os
from typing import TYPE_CHECKING, overload

if TYPE_CHECKING:
    from _typeshed import StrPath
    from typing_extensions import LiteralString, reveal_type

@overload
def insert_before_extension(
    filename: LiteralString | os.PathLike[LiteralString], content: LiteralString
) -> LiteralString: ...
@overload
def insert_before_extension(filename: StrPath, content: str) -> str: ...
def insert_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
  • Python version used: 3.12.6

Activity

  1. hamdanal commented on Feb 2, 2025

    @hamdanal
    Collaborator

    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.

    Duplicate of #12554

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions