Skip to content

Commit eb81573

Browse files
committed
Conformance: minor update to LiteralString tests
I ran into this while looking at ty's conformance results. For an expression like `str(1)`, ty infers `Literal["1"]`. It can therefore infer that `some_literal_str + str(1)` is also of type `LiteralString`. The conformance suite asserts the opposite (that the result should be of type `str`), based on the assumption that `str(1)` is definitely of type `str`. The updated code also seems like a readability-improvement to me, as there is now just one way to refer to a non-`LiteralStr` object. The updates here only lead to line-number changes in the results for all type checkers.
1 parent ffd520a commit eb81573

File tree

5 files changed

+34
-35
lines changed

5 files changed

+34
-35
lines changed

conformance/results/mypy/literals_literalstring.toml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ output = """
66
literals_literalstring.py:36: error: Parameter 2 of Literal[...] is invalid [valid-type]
77
literals_literalstring.py:37: error: Parameter 1 of Literal[...] is invalid [valid-type]
88
literals_literalstring.py:43: error: Incompatible types in assignment (expression has type "Literal['two']", variable has type "Literal['']") [assignment]
9-
literals_literalstring.py:74: error: Incompatible types in assignment (expression has type "int", variable has type "str") [assignment]
10-
literals_literalstring.py:75: error: Incompatible types in assignment (expression has type "bytes", variable has type "str") [assignment]
11-
literals_literalstring.py:157: error: Overloaded function signature 3 will never be matched: signature 2's parameter type(s) are the same or broader [overload-cannot-match]
12-
literals_literalstring.py:167: error: Expression is of type "B", not "A" [assert-type]
9+
literals_literalstring.py:73: error: Incompatible types in assignment (expression has type "int", variable has type "str") [assignment]
10+
literals_literalstring.py:74: error: Incompatible types in assignment (expression has type "bytes", variable has type "str") [assignment]
11+
literals_literalstring.py:156: error: Overloaded function signature 3 will never be matched: signature 2's parameter type(s) are the same or broader [overload-cannot-match]
12+
literals_literalstring.py:166: error: Expression is of type "B", not "A" [assert-type]
1313
"""
1414
conformance_automated = "Fail"
1515
errors_diff = """
16-
Line 66: Expected 1 errors
17-
Line 120: Expected 1 errors
18-
Line 134: Expected 1 errors
19-
Line 171: Expected 1 errors
20-
Line 157: Unexpected errors ["literals_literalstring.py:157: error: Overloaded function signature 3 will never be matched: signature 2's parameter type(s) are the same or broader [overload-cannot-match]"]
21-
Line 167: Unexpected errors ['literals_literalstring.py:167: error: Expression is of type "B", not "A" [assert-type]']
16+
Line 65: Expected 1 errors
17+
Line 119: Expected 1 errors
18+
Line 133: Expected 1 errors
19+
Line 170: Expected 1 errors
20+
Line 156: Unexpected errors ["literals_literalstring.py:156: error: Overloaded function signature 3 will never be matched: signature 2's parameter type(s) are the same or broader [overload-cannot-match]"]
21+
Line 166: Unexpected errors ['literals_literalstring.py:166: error: Expression is of type "B", not "A" [assert-type]']
2222
"""

conformance/results/pyrefly/literals_literalstring.toml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ output = """
55
ERROR literals_literalstring.py:36:29-42: Invalid type inside literal, `LiteralString` [invalid-literal]
66
ERROR literals_literalstring.py:37:22-35: Invalid type inside literal, `LiteralString` [invalid-literal]
77
ERROR literals_literalstring.py:43:23-24: `Literal['two']` is not assignable to `Literal['']` [bad-assignment]
8-
ERROR literals_literalstring.py:66:25-47: `str` is not assignable to `LiteralString` [bad-assignment]
9-
ERROR literals_literalstring.py:74:25-26: `Literal[3]` is not assignable to `LiteralString` [bad-assignment]
10-
ERROR literals_literalstring.py:75:25-32: `Literal[b'test']` is not assignable to `LiteralString` [bad-assignment]
11-
ERROR literals_literalstring.py:120:21-24: `str` is not assignable to upper bound `LiteralString` of type variable `TLiteral` [bad-specialization]
12-
ERROR literals_literalstring.py:134:51-52: Argument `str` is not assignable to parameter `value` with type `LiteralString` in function `Container.__init__` [bad-argument-type]
13-
ERROR literals_literalstring.py:171:21-24: `list[LiteralString]` is not assignable to `list[str]` [bad-assignment]
8+
ERROR literals_literalstring.py:65:25-45: `str` is not assignable to `LiteralString` [bad-assignment]
9+
ERROR literals_literalstring.py:73:25-26: `Literal[3]` is not assignable to `LiteralString` [bad-assignment]
10+
ERROR literals_literalstring.py:74:25-32: `Literal[b'test']` is not assignable to `LiteralString` [bad-assignment]
11+
ERROR literals_literalstring.py:119:21-24: `str` is not assignable to upper bound `LiteralString` of type variable `TLiteral` [bad-specialization]
12+
ERROR literals_literalstring.py:133:51-52: Argument `str` is not assignable to parameter `value` with type `LiteralString` in function `Container.__init__` [bad-argument-type]
13+
ERROR literals_literalstring.py:170:21-24: `list[LiteralString]` is not assignable to `list[str]` [bad-assignment]
1414
"""

conformance/results/pyright/literals_literalstring.toml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ literals_literalstring.py:36:29 - error: Type arguments for "Literal" must be No
44
literals_literalstring.py:37:22 - error: Type arguments for "Literal" must be None, a literal value (int, bool, str, or bytes), or an enum value (reportInvalidTypeForm)
55
literals_literalstring.py:43:23 - error: Type "Literal['two']" is not assignable to declared type "Literal['']"
66
  "Literal['two']" is not assignable to type "Literal['']" (reportAssignmentType)
7-
literals_literalstring.py:66:25 - error: Type "str" is not assignable to declared type "LiteralString"
7+
literals_literalstring.py:65:25 - error: Type "str" is not assignable to declared type "LiteralString"
88
  "str" is not assignable to "LiteralString" (reportAssignmentType)
9-
literals_literalstring.py:74:25 - error: Type "Literal[3]" is not assignable to declared type "LiteralString"
9+
literals_literalstring.py:73:25 - error: Type "Literal[3]" is not assignable to declared type "LiteralString"
1010
  "Literal[3]" is not assignable to "LiteralString" (reportAssignmentType)
11-
literals_literalstring.py:75:25 - error: Type "Literal[b"test"]" is not assignable to declared type "LiteralString"
11+
literals_literalstring.py:74:25 - error: Type "Literal[b"test"]" is not assignable to declared type "LiteralString"
1212
  "Literal[b"test"]" is not assignable to "LiteralString" (reportAssignmentType)
13-
literals_literalstring.py:120:22 - error: Argument of type "str" cannot be assigned to parameter "s" of type "TLiteral@literal_identity" in function "literal_identity"
13+
literals_literalstring.py:119:22 - error: Argument of type "str" cannot be assigned to parameter "s" of type "TLiteral@literal_identity" in function "literal_identity"
1414
  Type "str" is not assignable to type "LiteralString"
1515
    "str" is not assignable to "LiteralString" (reportArgumentType)
16-
literals_literalstring.py:134:51 - error: Argument of type "str" cannot be assigned to parameter "value" of type "T@Container" in function "__init__"
16+
literals_literalstring.py:133:51 - error: Argument of type "str" cannot be assigned to parameter "value" of type "T@Container" in function "__init__"
1717
  Type "str" is not assignable to type "LiteralString"
1818
    "str" is not assignable to "LiteralString" (reportArgumentType)
19-
literals_literalstring.py:171:21 - error: Type "list[LiteralString]" is not assignable to declared type "list[str]"
19+
literals_literalstring.py:170:21 - error: Type "list[LiteralString]" is not assignable to declared type "list[str]"
2020
  "list[LiteralString]" is not assignable to "list[str]"
2121
    Type parameter "_T@list" is invariant, but "LiteralString" is not the same as "str"
2222
    Consider switching from "list" to "Sequence" which is covariant (reportAssignmentType)

conformance/results/zuban/literals_literalstring.toml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ output = """
55
literals_literalstring.py:36: error: Parameter 2 of Literal[...] is invalid [valid-type]
66
literals_literalstring.py:37: error: Parameter 1 of Literal[...] is invalid [valid-type]
77
literals_literalstring.py:43: error: Incompatible types in assignment (expression has type "Literal['two']", variable has type "Literal['']") [assignment]
8-
literals_literalstring.py:66: error: Incompatible types in assignment (expression has type "str", variable has type "LiteralString") [assignment]
9-
literals_literalstring.py:74: error: Incompatible types in assignment (expression has type "Literal[3]", variable has type "LiteralString") [assignment]
10-
literals_literalstring.py:75: error: Incompatible types in assignment (expression has type "Literal[b'test']", variable has type "LiteralString") [assignment]
11-
literals_literalstring.py:120: error: Value of type variable "TLiteral" of "literal_identity" cannot be "str" [type-var]
12-
literals_literalstring.py:134: error: Value of type variable "T" of "Container" cannot be "str" [type-var]
13-
literals_literalstring.py:171: error: Incompatible types in assignment (expression has type "list[LiteralString]", variable has type "list[str]") [assignment]
14-
literals_literalstring.py:171: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
15-
literals_literalstring.py:171: note: Consider using "Sequence" instead, which is covariant
8+
literals_literalstring.py:65: error: Incompatible types in assignment (expression has type "str", variable has type "LiteralString") [assignment]
9+
literals_literalstring.py:73: error: Incompatible types in assignment (expression has type "Literal[3]", variable has type "LiteralString") [assignment]
10+
literals_literalstring.py:74: error: Incompatible types in assignment (expression has type "Literal[b'test']", variable has type "LiteralString") [assignment]
11+
literals_literalstring.py:119: error: Value of type variable "TLiteral" of "literal_identity" cannot be "str" [type-var]
12+
literals_literalstring.py:133: error: Value of type variable "T" of "Container" cannot be "str" [type-var]
13+
literals_literalstring.py:170: error: Incompatible types in assignment (expression has type "list[LiteralString]", variable has type "list[str]") [assignment]
14+
literals_literalstring.py:170: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
15+
literals_literalstring.py:170: note: Consider using "Sequence" instead, which is covariant
1616
"""

conformance/tests/literals_literalstring.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ def func1(a: Literal["one"], b: Literal["two"]):
4343
x2: Literal[""] = b # E
4444

4545

46-
def func2(a: LiteralString, b: LiteralString):
46+
def func2(a: LiteralString, b: LiteralString, non_literal: str):
4747
# > Addition: x + y is of type LiteralString if both x and y are compatible with LiteralString.
4848
assert_type(a + b, LiteralString)
4949

5050
# > Joining: sep.join(xs) is of type LiteralString if sep’s type is
5151
# > compatible with LiteralString and xs’s type is compatible with Iterable[LiteralString].
5252
assert_type(",".join((a, b)), LiteralString)
53-
assert_type(",".join((a, str(b))), str)
53+
assert_type(",".join((a, non_literal)), str)
5454

5555
# > In-place addition: If s has type LiteralString and x has type compatible with
5656
# > LiteralString, then s += x preserves s’s type as LiteralString.
@@ -62,10 +62,9 @@ def func2(a: LiteralString, b: LiteralString):
6262
# > s and the arguments have types compatible with LiteralString.
6363
assert_type(f"{a} {b}", LiteralString)
6464

65-
variable = 3
66-
x1: LiteralString = f"{a} {str(variable)}" # E
65+
x1: LiteralString = f"{a} {non_literal}" # E
6766

68-
assert_type(a + str(1), str)
67+
assert_type(a + non_literal, str)
6968

7069
# > LiteralString is compatible with the type str
7170
x2: str = a

0 commit comments

Comments
 (0)