Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve mdtests style #14884

Merged
merged 5 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ x: Subclass = 1 # error: [invalid-assignment]
# TODO: no diagnostic
y: int = Subclass() # error: [invalid-assignment]

def f() -> Subclass:
pass

reveal_type(f()) # revealed: Subclass
def _(s: Subclass) -> None:
InSyncWithFoo marked this conversation as resolved.
Show resolved Hide resolved
reveal_type(s) # revealed: Subclass
```
Original file line number Diff line number Diff line change
Expand Up @@ -89,28 +89,26 @@ vice versa.
```py
from typing_extensions import Literal, LiteralString

def coinflip() -> bool:
return True
def _(flag: bool) -> None:
foo_1: Literal["foo"] = "foo"
bar_1: LiteralString = foo_1 # fine

foo_1: Literal["foo"] = "foo"
bar_1: LiteralString = foo_1 # fine
foo_2 = "foo" if flag else "bar"
reveal_type(foo_2) # revealed: Literal["foo", "bar"]
bar_2: LiteralString = foo_2 # fine

foo_2 = "foo" if coinflip() else "bar"
reveal_type(foo_2) # revealed: Literal["foo", "bar"]
bar_2: LiteralString = foo_2 # fine
foo_3: LiteralString = "foo" * 1_000_000_000
bar_3: str = foo_2 # fine

foo_3: LiteralString = "foo" * 1_000_000_000
bar_3: str = foo_2 # fine
baz_1: str = str()
qux_1: LiteralString = baz_1 # error: [invalid-assignment]

baz_1: str = str()
qux_1: LiteralString = baz_1 # error: [invalid-assignment]
baz_2: LiteralString = "baz" * 1_000_000_000
qux_2: Literal["qux"] = baz_2 # error: [invalid-assignment]

baz_2: LiteralString = "baz" * 1_000_000_000
qux_2: Literal["qux"] = baz_2 # error: [invalid-assignment]

baz_3 = "foo" if coinflip() else 1
reveal_type(baz_3) # revealed: Literal["foo"] | Literal[1]
qux_3: LiteralString = baz_3 # error: [invalid-assignment]
baz_3 = "foo" if flag else 1
reveal_type(baz_3) # revealed: Literal["foo"] | Literal[1]
qux_3: LiteralString = baz_3 # error: [invalid-assignment]
```

### Narrowing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,152 +3,122 @@
## Simple

```py
def f() -> "int":
return 1

reveal_type(f()) # revealed: int
def f(v: "int") -> None:
reveal_type(v) # revealed: int
```

## Nested

```py
def f() -> "'int'":
return 1

reveal_type(f()) # revealed: int
def f(v: "'int'") -> None:
reveal_type(v) # revealed: int
```

## Type expression

```py
def f1() -> "int | str":
return 1

def f2() -> "tuple[int, str]":
return 1
def f1(v: "int | str") -> None:
reveal_type(v) # revealed: int | str

reveal_type(f1()) # revealed: int | str
reveal_type(f2()) # revealed: tuple[int, str]
def f2(v: "tuple[int, str]") -> None:
reveal_type(v) # revealed: tuple[int, str]
```

## Partial

```py
def f() -> tuple[int, "str"]:
return 1

reveal_type(f()) # revealed: tuple[int, str]
def f(v: tuple[int, "str"]) -> None:
reveal_type(v) # revealed: tuple[int, str]
```

## Deferred

```py
def f() -> "Foo":
return Foo()
def f(v: "Foo") -> None:
reveal_type(v) # revealed: Foo

class Foo:
pass

reveal_type(f()) # revealed: Foo
```

## Deferred (undefined)

```py
# error: [unresolved-reference]
def f() -> "Foo":
pass

reveal_type(f()) # revealed: Unknown
def f(v: "Foo") -> None:
reveal_type(v) # revealed: Unknown
```

## Partial deferred

```py
def f() -> int | "Foo":
return 1
def f(v: int | "Foo") -> None:
reveal_type(v) # revealed: int | Foo

class Foo:
pass

reveal_type(f()) # revealed: int | Foo
```

## `typing.Literal`

```py
from typing import Literal

def f1() -> Literal["Foo", "Bar"]:
return "Foo"
def f1(v: Literal["Foo", "Bar"]) -> None:
reveal_type(v) # revealed: Literal["Foo", "Bar"]

def f2() -> 'Literal["Foo", "Bar"]':
return "Foo"
def f2(v: 'Literal["Foo", "Bar"]') -> None:
reveal_type(v) # revealed: Literal["Foo", "Bar"]

class Foo:
pass

reveal_type(f1()) # revealed: Literal["Foo", "Bar"]
reveal_type(f2()) # revealed: Literal["Foo", "Bar"]
```

## Various string kinds

```py
# error: [annotation-raw-string] "Type expressions cannot use raw string literal"
def f1() -> r"int":
return 1
def f1(v: r"int") -> None:
reveal_type(v) # revealed: Unknown

# error: [annotation-f-string] "Type expressions cannot use f-strings"
def f2() -> f"int":
return 1
def f2(v: f"int") -> None:
reveal_type(v) # revealed: Unknown

# error: [annotation-byte-string] "Type expressions cannot use bytes literal"
def f3() -> b"int":
return 1
def f3(v: b"int") -> None:
reveal_type(v) # revealed: Unknown

def f4() -> "int":
return 1
def f4(v: "int") -> None:
reveal_type(v) # revealed: int

# error: [annotation-implicit-concat] "Type expressions cannot span multiple string literals"
def f5() -> "in" "t":
return 1
def f5(v: "in" "t") -> None:
reveal_type(v) # revealed: Unknown

# error: [annotation-escape-character] "Type expressions cannot contain escape characters"
def f6() -> "\N{LATIN SMALL LETTER I}nt":
return 1
def f6(v: "\N{LATIN SMALL LETTER I}nt") -> None:
reveal_type(v) # revealed: Unknown

# error: [annotation-escape-character] "Type expressions cannot contain escape characters"
def f7() -> "\x69nt":
return 1
def f7(v: "\x69nt") -> None:
reveal_type(v) # revealed: Unknown

def f8() -> """int""":
return 1
def f8(v: """int""") -> None:
reveal_type(v) # revealed: int

# error: [annotation-byte-string] "Type expressions cannot use bytes literal"
def f9() -> "b'int'":
return 1

reveal_type(f1()) # revealed: Unknown
reveal_type(f2()) # revealed: Unknown
reveal_type(f3()) # revealed: Unknown
reveal_type(f4()) # revealed: int
reveal_type(f5()) # revealed: Unknown
reveal_type(f6()) # revealed: Unknown
reveal_type(f7()) # revealed: Unknown
reveal_type(f8()) # revealed: int
reveal_type(f9()) # revealed: Unknown
def f9(v: "b'int'") -> None:
reveal_type(v) # revealed: Unknown
```

## Various string kinds in `typing.Literal`

```py
from typing import Literal

def f() -> Literal["a", r"b", b"c", "d" "e", "\N{LATIN SMALL LETTER F}", "\x67", """h"""]:
return "normal"

reveal_type(f()) # revealed: Literal["a", "b", "de", "f", "g", "h"] | Literal[b"c"]
def f(v: Literal["a", r"b", b"c", "d" "e", "\N{LATIN SMALL LETTER F}", "\x67", """h"""]) -> None:
reveal_type(v) # revealed: Literal["a", "b", "de", "f", "g", "h"] | Literal[b"c"]
```

## Class variables
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,14 @@ c: tuple[str | int, str] = ([], "foo")
## PEP-604 annotations are supported

```py
def foo() -> str | int | None:
return None
def foo(v: str | int | None) -> None:
reveal_type(v) # revealed: str | int | None

reveal_type(foo()) # revealed: str | int | None
def bar(v: str | str | None) -> None:
reveal_type(v) # revealed: str | None

def bar() -> str | str | None:
return None

reveal_type(bar()) # revealed: str | None

def baz() -> str | str:
return "Hello, world!"

reveal_type(baz()) # revealed: str
def baz(v: str | str) -> None:
reveal_type(v) # revealed: str
```

## Attribute expressions in type annotations are understood
Expand Down
Loading
Loading