microsoft / pyright

Static Type Checker for Python
Other
13.12k stars 1.4k forks source link

Issues with Tuple of Tuples Inference #8819

Closed max-muoto closed 3 weeks ago

max-muoto commented 3 weeks ago

I would generally expect a tuple of tuples to retain any literal types within the nested tuples. Take this example, which fails:

from typing import LiteralString

def foo(table: LiteralString, field: LiteralString) -> None: ...

for table, field in (("metric", "id"), ("test", "test")):
    # Warning, as these aren't literals.
    foo(table, field)

I can understand for why a mutable collection, the tuples literal values would be discarded, but in this case I'd imagine they should be preserved? I rely fairly heavily on LiteralStrings for constructing raw SQL (some drivers such as Pyscopg disallow raw strings to be passed along when executing SQL), so this was an edge case I ran into.

Pyright Playground: https://pyright-play.net/?pythonVersion=3.14&strict=true&code=GYJw9gtgBALgngBwJYDsDmUkQWEMoAySMApiAIYA2AyjCKmgFDMAmJwUwYYAFDOQCNKJAFyFiZKrXroANJyQlKLMUVIUadBgEooAWgB8UAHJgUoqADprzRlxCxBw%2BcEXLMKKDx4AiCCS0AYx95HyQWH215X1IAZxgQqB84hO1tEUYoLM5uPicSFzcWbVsYAFcEKABeL19-IMSwiKivZJJ4xpTIkpASADcSKgB9eAQSPgrtIA

Version: 1.1.377

erictraut commented 3 weeks ago

Pyright is working as intended here, so I don't consider this a bug.

We have on occasion changed inference rules, but this is done only after much deliberation and evidence that it's merited. Each such change carries with it the potential for introducing new false positives in typed code bases, so the bar is very high.

I'm going to close this issue for now, but it's something that we could consider changing in the future if there's sufficient demand / justification.

max-muoto commented 3 weeks ago

Pyright is working as intended here, so I don't consider this a bug.

We have on occasion changed inference rules, but this is done only after much deliberation and evidence that it's merited. Each such change carries with it the potential for introducing new false positives in typed code bases, so the bar is very high.

I'm going to close this issue for now, but it's something that we could consider changing in the future if there's sufficient demand / justification.

Sounds good! Definitely an outlier in terms of needs here, so makes sense.