Skip to content

Commit

Permalink
Merge branch 'main' into literal-type
Browse files Browse the repository at this point in the history
  • Loading branch information
Glyphack authored Oct 28, 2024
2 parents 8bb47be + 1f19aca commit 3fc52a5
Show file tree
Hide file tree
Showing 190 changed files with 1,665 additions and 1,979 deletions.
86 changes: 42 additions & 44 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ Ruff is used by a number of major open-source projects and companies, including:
- [Babel](https://github.com/python-babel/babel)
- Benchling ([Refac](https://github.com/benchling/refac))
- [Bokeh](https://github.com/bokeh/bokeh)
- CrowdCent ([NumerBlox](https://github.com/crowdcent/numerblox)) <!-- typos: ignore -->
- [Cryptography (PyCA)](https://github.com/pyca/cryptography)
- CERN ([Indico](https://getindico.io/))
- [DVC](https://github.com/iterative/dvc)
Expand Down
1 change: 1 addition & 0 deletions _typos.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pn = "pn" # `import panel as pn` is a thing
poit = "poit"
BA = "BA" # acronym for "Bad Allowed", used in testing.
jod = "jod" # e.g., `jod-thread`
Numer = "Numer" # Library name 'NumerBlox' in "Who's Using Ruff?"

[default]
extend-ignore-re = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,21 @@ x: int
x = "foo" # error: [invalid-assignment] "Object of type `Literal["foo"]` is not assignable to `int`"
```

## PEP-604 annotations not yet supported
## PEP-604 annotations are supported

```py
def f() -> str | None:
def foo() -> str | int | None:
return None

# TODO: should be `str | None` (but Todo is better than `Unknown`)
reveal_type(f()) # revealed: @Todo
reveal_type(foo()) # revealed: str | int | 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
```
16 changes: 11 additions & 5 deletions crates/red_knot_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3584,14 +3584,20 @@ impl<'db> TypeInferenceBuilder<'db> {

ast::Expr::Subscript(subscript) => self.infer_subscript_type_expression(subscript),

// TODO PEP-604 unions
ast::Expr::BinOp(binary) => {
self.infer_binary_expression(binary);
#[allow(clippy::single_match_else)]
match binary.op {
// PEP-604 unions are okay
ast::Operator::BitOr => Type::Todo,
// PEP-604 unions are okay, e.g., `int | str`
ast::Operator::BitOr => {
let left_ty = self.infer_type_expression(&binary.left);
let right_ty = self.infer_type_expression(&binary.right);
UnionType::from_elements(self.db, [left_ty, right_ty])
}
// anything else is an invalid annotation:
_ => Type::Unknown,
_ => {
self.infer_binary_expression(binary);
Type::Unknown
}
}
}

Expand Down
Loading

0 comments on commit 3fc52a5

Please sign in to comment.