[ty] fix inferring type variable from string literal argument#23326
[ty] fix inferring type variable from string literal argument#23326AlexWaygood merged 4 commits intomainfrom
Conversation
Typing conformance resultsNo changes detected ✅ |
|
Memory usage reportSummary
Significant changesClick to expand detailed breakdownprefect
sphinx
trio
flake8
|
|
seems like the bug also exists for bytes-literals from typing import Iterable, TypeVar
FlatT = TypeVar("FlatT")
def flatten(*iterables: Iterable[FlatT]) -> list[FlatT]:
return [x for iterable in iterables for x in iterable]
reveal_type(flatten(b"abc", ("a", "b", "c"))) |
|
What about this small variation: from typing import Iterable, TypeVar
FlatT = TypeVar("FlatT")
def flatten_covariant(*iterables: Iterable[FlatT]) -> tuple[FlatT, ...]:
return tuple(x for iterable in iterables for x in iterable)
reveal_type(flatten_covariant("abc", (1, 2, 3))) # revealed: tuple[str | Literal[1, 2, 3], ...]Wouldn't |
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
unsupported-operator |
87 | 0 | 2 |
invalid-argument-type |
3 | 1 | 43 |
unresolved-attribute |
5 | 0 | 1 |
invalid-assignment |
1 | 0 | 0 |
not-iterable |
0 | 0 | 1 |
type-assertion-failure |
0 | 0 | 1 |
| Total | 96 | 1 | 48 |
Yes... definitely adds implementation complexity though. I'll see what I can come up with quickly, but I might be in favor of deferring this, definitely seems lower priority than the original issue. |
|
Still noodling on the extension to infer sequence of literals in the covariant case (pyright doesn't do this fwiw). I think it's doable but there will be changes in several different areas. The core problem is that our specialization inference precision cannot "get ahead" of our has-relation-to capabilities. Currently in protocol matching, string literals fall back to So I'd like to defer all of that to a separate PR, and land this PR as-is. I added tests with TODO comments. |
189b51e to
0a8de25
Compare
* main: (43 commits) [`ruff`] Suppress diagnostic for strings with backslashes in interpolations before Python 3.12 (`RUF027`) (#21069) [flake8-bugbear] Fix B023 false positive for immediately-invoked lambdas (#23294) [ty] Add `Final` mdtests for loops and redeclaration (#23331) [`flake8-pyi`] Also check string annotations (`PYI041`) (#19023) Remove AlexWaygood as a flake8-pyi codeowner (#23347) [ty] Add comments to clarify the purpose of `NominalInstanceType::class_name` and `NominalInstanceType::class_module_name` (#23339) Add attestations for release artifacts and Docker images (#23111) [ty] Fix `assert_type` diagnostic messages (#23342) [ty] Force-update all insta snapshots (#23343) Add Q004 to the list of conflicting rules (#23340) [ty] Fix `invalid-match-pattern` false positives (#23338) [ty] new diagnostic called-match-pattern-must-be-a-type (#22939) [ty] Update flaky projects (#23337) [ty] Increase timeout for ecosystem report to 40 min (#23336) Bump ecosystem-analyzer pin (#23335) [ty] Replace `strsim` with CPython-based Levenshtein implementation (#23291) [ty] Add mdtest for staticmethod assigned in class body (#23330) [ty] fix inferring type variable from string literal argument (#23326) [ty] bytes literal is a sequence of integers (#23329) Update rand and getrandom (#23333) ...
Summary
Fixes astral-sh/ty#2821
Allow a string literal argument to match against an iterable parameter in typevar inference.
Test Plan
Added mdtest.