[ty] Tighten up validation of subscripts and attributes in type expressions#24329
[ty] Tighten up validation of subscripts and attributes in type expressions#24329AlexWaygood merged 2 commits intomainfrom
Conversation
Typing conformance resultsThe percentage of diagnostics emitted that were expected errors held steady at 86.76%. The percentage of expected errors that received a diagnostic held steady at 81.53%. The number of fully passing files held steady at 70/132. SummaryHow are test cases classified?Each test case represents one expected error annotation or a group of annotations sharing a tag. Counts are per test case, not per diagnostic — multiple diagnostics on the same line count as one. Required annotations (
True positives changed (5)5 diagnostics
|
Memory usage reportMemory usage unchanged ✅ |
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
invalid-type-form |
0 | 0 | 5 |
| Total | 0 | 0 | 5 |
Raw diff:
egglog-python (https://github.com/egraphs-good/egglog-python)
- python/egglog/builtins.py:542:79 error[invalid-type-form] Invalid subscript of object of type `tuple[type, ...]` in type expression
+ python/egglog/builtins.py:542:79 error[invalid-type-form] Only simple names and dotted names can be subscripted in type expressions
- python/egglog/builtins.py:666:89 error[invalid-type-form] Invalid subscript of object of type `tuple[type, ...]` in type expression
+ python/egglog/builtins.py:666:89 error[invalid-type-form] Only simple names and dotted names can be subscripted in type expressions
- python/egglog/builtins.py:1060:83 error[invalid-type-form] Invalid subscript of object of type `tuple[type, ...]` in type expression
+ python/egglog/builtins.py:1060:83 error[invalid-type-form] Only simple names and dotted names can be subscripted in type expressions
- python/tests/test_convert.py:117:35 error[invalid-type-form] Invalid subscript of object of type `tuple[type, ...]` in type expression
+ python/tests/test_convert.py:117:35 error[invalid-type-form] Only simple names and dotted names can be subscripted in type expressions
- python/tests/test_convert.py:140:33 error[invalid-type-form] Invalid subscript of object of type `tuple[type, ...]` in type expression
+ python/tests/test_convert.py:140:33 error[invalid-type-form] Only simple names and dotted names can be subscripted in type expressions
|
11e34ab to
4e832cc
Compare
There was a problem hiding this comment.
I agree this is what is specified (though it took me a few minutes to notice the extra clarification underneath the grammar table about name being allowed to be a qualified dotted name). It's perhaps worth noting that currently only mypy enforces this rule, so this PR puts us into the strictest tier with mypy (and zuban). Pyright and pyrefly and pycroscope are all fine with this, for example:
class C[T]:
class Inner: pass
x: C[int].Inner = C.Inner()Ecosystem results suggest it's not a problem in practice to be strict about this, though if someone proposed that the above should be allowed by the spec, I would not argue against allowing it. Which I guess suggests that my initial inclination is that it ought to be allowed.
| # error: [invalid-type-form] "Invalid subscript" | ||
| # error: [invalid-type-form] "Only simple names and dotted names can be subscripted in type expressions" | ||
| o: "[1, 2, 3][1:2]", | ||
| # error: [invalid-type-form] "Only simple names, dotted names and subscripts can be used in type expressions" |
There was a problem hiding this comment.
The wording of this message is a bit confusing, because the expression below could reasonably be said to contain only "simple names, dotted names, and subscripts". The subtlety is that "dotted names" can't include a subscript. Not sure how to make that subtlety clearer, though...
There was a problem hiding this comment.
well... the expression below might contain only "simple names, dotted names and subscripts", but it is nonetheless not a simple name, dotted name or subscript itself! (It's a non-dotted-name attribute expression where the value is a subscript.)
But, uh, yeah, possibly not my finest error message... I also don't really have any better ideas, though. We do link to the typing spec grammar in a subdiagnostic, so that's something at least
Sure, but is that actually useful? It "means" exactly the same thing as The rules in the typing spec make sense to me here; I'm in favour of strictly enforcing them. |
Summary
According to the type-expression grammar in the typing spec, only simple names and dotted names can be subscripted in type expressions. More complex expressions, such as
list[T][int], constitute invalid type expressions. This PR adds the missing validation for this rule.Test Plan
mdtests