Skip to content

Commit

Permalink
Merge branch 'main' into dcreager/module-resolution
Browse files Browse the repository at this point in the history
* main: (25 commits)
  [`pydocstyle`] Skip leading whitespace for `D403` (#14963)
  Update pre-commit dependencies (#15008)
  Check diagnostic refresh support from client capability (#15014)
  Update Rust crate colored to v2.2.0 (#15010)
  Update dependency monaco-editor to v0.52.2 (#15006)
  Update Rust crate thiserror to v2.0.7 (#15005)
  Update Rust crate serde to v1.0.216 (#15004)
  Update Rust crate libc to v0.2.168 (#15003)
  Update Rust crate fern to v0.7.1 (#15002)
  Update Rust crate chrono to v0.4.39 (#15001)
  Update Rust crate bstr to v1.11.1 (#15000)
  Update NPM Development dependencies (#14999)
  Update dependency ruff to v0.8.3 (#15007)
  Pin mdformat plugins in pre-commit (#14992)
  Use stripping block (`|-`) for page descriptions (#14980)
  [`perflint`] Fix panic in `perf401` (#14971)
  Improve the documentation of E201/E202 (#14983)
  [ruff_python_ast] Add name and default functions to TypeParam. (#14964)
  [red-knot] Emit an error if a bare `Annotated` or `Literal` is used in a type expression (#14973)
  [red-knot] Fix bugs relating to assignability of dynamic `type[]` types (#14972)
  ...
  • Loading branch information
dcreager committed Dec 16, 2024
2 parents ba828fd + 6a5eff6 commit ff5432a
Show file tree
Hide file tree
Showing 67 changed files with 1,131 additions and 532 deletions.
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ repos:
hooks:
- id: mdformat
additional_dependencies:
- mdformat-mkdocs
- mdformat-admon
- mdformat-footnote
- mdformat-mkdocs<4.0.0
- mdformat-admon==2.0.6
- mdformat-footnote==0.1.1
exclude: |
(?x)^(
docs/formatter/black\.md
Expand Down Expand Up @@ -59,7 +59,7 @@ repos:
- black==24.10.0

- repo: https://github.com/crate-ci/typos
rev: v1.28.2
rev: v1.28.3
hooks:
- id: typos

Expand All @@ -73,7 +73,7 @@ repos:
pass_filenames: false # This makes it a lot faster

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.2
rev: v0.8.3
hooks:
- id: ruff-format
- id: ruff
Expand Down
58 changes: 29 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,24 @@ It is invalid to parameterize `Annotated` with less than two arguments.
```py
from typing_extensions import Annotated

# TODO: This should be an error
# error: [invalid-type-form] "`Annotated` requires at least two arguments when used in an annotation or type expression"
def _(x: Annotated):
reveal_type(x) # revealed: Unknown

def _(flag: bool):
if flag:
X = Annotated
else:
X = bool

# error: [invalid-type-form] "`Annotated` requires at least two arguments when used in an annotation or type expression"
def f(y: X):
reveal_type(y) # revealed: Unknown | bool

# error: [invalid-type-form] "`Annotated` requires at least two arguments when used in an annotation or type expression"
def _(x: Annotated | bool):
reveal_type(x) # revealed: Unknown | bool

# error: [invalid-type-form]
def _(x: Annotated[()]):
reveal_type(x) # revealed: Unknown
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,13 @@ a1: Literal[26]
def f():
reveal_type(a1) # revealed: Literal[26]
```

## Invalid

```py
from typing import Literal

# error: [invalid-type-form] "`Literal` requires at least one argument when used in a type expression"
def _(x: Literal):
reveal_type(x) # revealed: Unknown
```
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
```py
import builtins

x = builtins.copyright
reveal_type(x) # revealed: Literal[copyright]
x = builtins.chr
reveal_type(x) # revealed: Literal[chr]
```
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ def returns_bool() -> bool:
return True

if returns_bool():
copyright = 1
chr = 1

def f():
reveal_type(copyright) # revealed: Literal[copyright] | Literal[1]
reveal_type(chr) # revealed: Literal[chr] | Literal[1]
```

## Conditionally global or builtin, with annotation
Expand All @@ -25,8 +25,8 @@ def returns_bool() -> bool:
return True

if returns_bool():
copyright: int = 1
chr: int = 1

def f():
reveal_type(copyright) # revealed: Literal[copyright] | int
reveal_type(chr) # revealed: Literal[chr] | int
```
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
# `type[Any]`

This file contains tests for non-fully-static `type[]` types, such as `type[Any]` and
`type[Unknown]`.

## Simple

```py
def f(x: type[Any]):
def f(x: type[Any], y: type[str]):
reveal_type(x) # revealed: type[Any]
# TODO: could be `<object.__repr__ type> & Any`
reveal_type(x.__repr__) # revealed: Any

# type[str] and type[Any] are assignable to each other
a: type[str] = x
b: type[Any] = y

class A: ...

x: type[Any] = object
Expand Down Expand Up @@ -70,3 +77,26 @@ def test(x: Any, y: SomethingUnknown):
reveal_type(y.__class__) # revealed: type[Unknown]
reveal_type(y.__class__.__class__.__class__.__class__) # revealed: type[Unknown]
```

## `type[Unknown]` has similar properties to `type[Any]`

```py
import abc
from typing import Any
from does_not_exist import SomethingUnknown # error: [unresolved-import]

has_unknown_type = SomethingUnknown.__class__
reveal_type(has_unknown_type) # revealed: type[Unknown]

def test(x: type[str], y: type[Any]):
"""Both `type[Any]` and `type[Unknown]` are assignable to all `type[]` types"""
a: type[Any] = x
b: type[str] = y
c: type[Any] = has_unknown_type
d: type[str] = has_unknown_type

def test2(a: type[Any]):
"""`type[Any]` and `type[Unknown]` are also assignable to all instances of `type` subclasses"""
b: abc.ABCMeta = a
b: abc.ABCMeta = has_unknown_type
```
Loading

0 comments on commit ff5432a

Please sign in to comment.