Skip to content

Commit

Permalink
Special-case ClassLiteral | ClassLiteral
Browse files Browse the repository at this point in the history
  • Loading branch information
dcreager committed Dec 27, 2024
1 parent 5239fa9 commit a52486f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ python-version = "3.10"
class A: ...
class B: ...

reveal_type(A | B) # revealed: UnionType
reveal_type(A | B) # revealed: Literal[A, B]
```

## Union of two classes (prior to 3.10)
Expand Down
8 changes: 7 additions & 1 deletion crates/red_knot_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ use crate::types::{
};
use crate::unpack::Unpack;
use crate::util::subscript::{PyIndex, PySlice};
use crate::Db;
use crate::{Db, Program, PythonVersion};

use super::context::{InferContext, WithDiagnostics};
use super::diagnostic::{
Expand Down Expand Up @@ -3361,6 +3361,12 @@ impl<'db> TypeInferenceBuilder<'db> {
(Type::Never, _, _) | (_, Type::Never, _) => Some(Type::Never),
(Type::Unknown, _, _) | (_, Type::Unknown, _) => Some(Type::Unknown),

(Type::ClassLiteral(_), Type::ClassLiteral(_), ast::Operator::BitOr)
if Program::get(self.db()).python_version(self.db()) >= PythonVersion::PY310 =>
{
Some(UnionType::from_elements(self.db(), [left_ty, right_ty]))
}

(Type::IntLiteral(n), Type::IntLiteral(m), ast::Operator::Add) => Some(
n.checked_add(m)
.map(Type::IntLiteral)
Expand Down

0 comments on commit a52486f

Please sign in to comment.