Skip to content

Commit e56f89f

Browse files
committed
Rename Type::Instance to Type::NominalInstance and InstanceType to NominalInstanceType
1 parent 4eecc40 commit e56f89f

File tree

10 files changed

+120
-105
lines changed

10 files changed

+120
-105
lines changed

crates/red_knot_python_semantic/src/symbol.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,7 @@ mod tests {
11141114
fn assert_bound_string_symbol<'db>(db: &'db dyn Db, symbol: Symbol<'db>) {
11151115
assert!(matches!(
11161116
symbol,
1117-
Symbol::Type(Type::Instance(_), Boundness::Bound)
1117+
Symbol::Type(Type::NominalInstance(_), Boundness::Bound)
11181118
));
11191119
assert_eq!(symbol.expect_type(), KnownClass::Str.to_instance(db));
11201120
}

crates/red_knot_python_semantic/src/types.rs

Lines changed: 74 additions & 65 deletions
Large diffs are not rendered by default.

crates/red_knot_python_semantic/src/types/builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ impl<'db> InnerIntersectionBuilder<'db> {
611611
Type::AlwaysFalsy if addition_is_bool_instance => {
612612
new_positive = Type::BooleanLiteral(false);
613613
}
614-
Type::Instance(instance)
614+
Type::NominalInstance(instance)
615615
if instance.class().is_known(db, KnownClass::Bool) =>
616616
{
617617
match new_positive {
@@ -722,7 +722,7 @@ impl<'db> InnerIntersectionBuilder<'db> {
722722
Type::Never => {
723723
// Adding ~Never to an intersection is a no-op.
724724
}
725-
Type::Instance(instance) if instance.class().is_object(db) => {
725+
Type::NominalInstance(instance) if instance.class().is_object(db) => {
726726
// Adding ~object to an intersection results in Never.
727727
*self = Self::default();
728728
self.positive.insert(Type::Never);

crates/red_knot_python_semantic/src/types/class_base.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,14 @@ impl<'db> ClassBase<'db> {
7878
Self::Class(literal.default_specialization(db))
7979
}),
8080
Type::GenericAlias(generic) => Some(Self::Class(ClassType::Generic(generic))),
81-
Type::Instance(instance) if instance.class().is_known(db, KnownClass::GenericAlias) => {
81+
Type::NominalInstance(instance)
82+
if instance.class().is_known(db, KnownClass::GenericAlias) =>
83+
{
8284
Self::try_from_type(db, todo_type!("GenericAlias instance"))
8385
}
8486
Type::Union(_) => None, // TODO -- forces consideration of multiple possible MROs?
8587
Type::Intersection(_) => None, // TODO -- probably incorrect?
86-
Type::Instance(_) => None, // TODO -- handle `__mro_entries__`?
88+
Type::NominalInstance(_) => None, // TODO -- handle `__mro_entries__`?
8789
Type::PropertyInstance(_) => None,
8890
Type::Never
8991
| Type::BooleanLiteral(_)

crates/red_knot_python_semantic/src/types/display.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,14 @@ impl Display for DisplayRepresentation<'_> {
7373
match self.ty {
7474
Type::Dynamic(dynamic) => dynamic.fmt(f),
7575
Type::Never => f.write_str("Never"),
76-
Type::Instance(instance) => match (instance.class(), instance.class().known(self.db)) {
77-
(_, Some(KnownClass::NoneType)) => f.write_str("None"),
78-
(_, Some(KnownClass::NoDefaultType)) => f.write_str("NoDefault"),
79-
(ClassType::NonGeneric(class), _) => f.write_str(&class.class(self.db).name),
80-
(ClassType::Generic(alias), _) => write!(f, "{}", alias.display(self.db)),
81-
},
76+
Type::NominalInstance(instance) => {
77+
match (instance.class(), instance.class().known(self.db)) {
78+
(_, Some(KnownClass::NoneType)) => f.write_str("None"),
79+
(_, Some(KnownClass::NoDefaultType)) => f.write_str("NoDefault"),
80+
(ClassType::NonGeneric(class), _) => f.write_str(&class.class(self.db).name),
81+
(ClassType::Generic(alias), _) => write!(f, "{}", alias.display(self.db)),
82+
}
83+
}
8284
Type::PropertyInstance(_) => f.write_str("property"),
8385
Type::ModuleLiteral(module) => {
8486
write!(f, "<module '{}'>", module.module(self.db).name())

crates/red_knot_python_semantic/src/types/infer.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,7 @@ impl<'db> TypeInferenceBuilder<'db> {
10651065
) -> bool {
10661066
match left {
10671067
Type::BooleanLiteral(_) | Type::IntLiteral(_) => {}
1068-
Type::Instance(instance)
1068+
Type::NominalInstance(instance)
10691069
if matches!(
10701070
instance.class().known(self.db()),
10711071
Some(KnownClass::Float | KnownClass::Int | KnownClass::Bool)
@@ -2518,7 +2518,7 @@ impl<'db> TypeInferenceBuilder<'db> {
25182518
}
25192519

25202520
// Super instances do not allow attribute assignment
2521-
Type::Instance(instance) if instance.class().is_known(db, KnownClass::Super) => {
2521+
Type::NominalInstance(instance) if instance.class().is_known(db, KnownClass::Super) => {
25222522
if emit_diagnostics {
25232523
if let Some(builder) = self.context.report_lint(&INVALID_ASSIGNMENT, target) {
25242524
builder.into_diagnostic(format_args!(
@@ -2543,7 +2543,7 @@ impl<'db> TypeInferenceBuilder<'db> {
25432543

25442544
Type::Dynamic(..) | Type::Never => true,
25452545

2546-
Type::Instance(..)
2546+
Type::NominalInstance(..)
25472547
| Type::BooleanLiteral(..)
25482548
| Type::IntLiteral(..)
25492549
| Type::StringLiteral(..)
@@ -3034,7 +3034,7 @@ impl<'db> TypeInferenceBuilder<'db> {
30343034
}
30353035

30363036
// Handle various singletons.
3037-
if let Type::Instance(instance) = declared_ty.inner_type() {
3037+
if let Type::NominalInstance(instance) = declared_ty.inner_type() {
30383038
if instance
30393039
.class()
30403040
.is_known(self.db(), KnownClass::SpecialForm)
@@ -5010,7 +5010,7 @@ impl<'db> TypeInferenceBuilder<'db> {
50105010
| Type::ClassLiteral(_)
50115011
| Type::GenericAlias(_)
50125012
| Type::SubclassOf(_)
5013-
| Type::Instance(_)
5013+
| Type::NominalInstance(_)
50145014
| Type::KnownInstance(_)
50155015
| Type::PropertyInstance(_)
50165016
| Type::Union(_)
@@ -5290,7 +5290,7 @@ impl<'db> TypeInferenceBuilder<'db> {
52905290
| Type::ClassLiteral(_)
52915291
| Type::GenericAlias(_)
52925292
| Type::SubclassOf(_)
5293-
| Type::Instance(_)
5293+
| Type::NominalInstance(_)
52945294
| Type::KnownInstance(_)
52955295
| Type::PropertyInstance(_)
52965296
| Type::Intersection(_)
@@ -5315,7 +5315,7 @@ impl<'db> TypeInferenceBuilder<'db> {
53155315
| Type::ClassLiteral(_)
53165316
| Type::GenericAlias(_)
53175317
| Type::SubclassOf(_)
5318-
| Type::Instance(_)
5318+
| Type::NominalInstance(_)
53195319
| Type::KnownInstance(_)
53205320
| Type::PropertyInstance(_)
53215321
| Type::Intersection(_)
@@ -5748,13 +5748,13 @@ impl<'db> TypeInferenceBuilder<'db> {
57485748
right_ty: right,
57495749
}),
57505750
},
5751-
(Type::IntLiteral(_), Type::Instance(_)) => self.infer_binary_type_comparison(
5751+
(Type::IntLiteral(_), Type::NominalInstance(_)) => self.infer_binary_type_comparison(
57525752
KnownClass::Int.to_instance(self.db()),
57535753
op,
57545754
right,
57555755
range,
57565756
),
5757-
(Type::Instance(_), Type::IntLiteral(_)) => self.infer_binary_type_comparison(
5757+
(Type::NominalInstance(_), Type::IntLiteral(_)) => self.infer_binary_type_comparison(
57585758
left,
57595759
op,
57605760
KnownClass::Int.to_instance(self.db()),
@@ -5880,7 +5880,7 @@ impl<'db> TypeInferenceBuilder<'db> {
58805880
KnownClass::Bytes.to_instance(self.db()),
58815881
range,
58825882
),
5883-
(Type::Tuple(_), Type::Instance(instance))
5883+
(Type::Tuple(_), Type::NominalInstance(instance))
58845884
if instance
58855885
.class()
58865886
.is_known(self.db(), KnownClass::VersionInfo) =>
@@ -5892,7 +5892,7 @@ impl<'db> TypeInferenceBuilder<'db> {
58925892
range,
58935893
)
58945894
}
5895-
(Type::Instance(instance), Type::Tuple(_))
5895+
(Type::NominalInstance(instance), Type::Tuple(_))
58965896
if instance
58975897
.class()
58985898
.is_known(self.db(), KnownClass::VersionInfo) =>
@@ -6271,7 +6271,7 @@ impl<'db> TypeInferenceBuilder<'db> {
62716271
) -> Type<'db> {
62726272
match (value_ty, slice_ty) {
62736273
(
6274-
Type::Instance(instance),
6274+
Type::NominalInstance(instance),
62756275
Type::IntLiteral(_) | Type::BooleanLiteral(_) | Type::SliceLiteral(_),
62766276
) if instance
62776277
.class()
@@ -6572,7 +6572,7 @@ impl<'db> TypeInferenceBuilder<'db> {
65726572
Err(_) => SliceArg::Unsupported,
65736573
},
65746574
Some(Type::BooleanLiteral(b)) => SliceArg::Arg(Some(i32::from(b))),
6575-
Some(Type::Instance(instance))
6575+
Some(Type::NominalInstance(instance))
65766576
if instance.class().is_known(self.db(), KnownClass::NoneType) =>
65776577
{
65786578
SliceArg::Arg(None)

crates/red_knot_python_semantic/src/types/instance.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,26 @@ use crate::Db;
55

66
impl<'db> Type<'db> {
77
pub(crate) const fn instance(class: ClassType<'db>) -> Self {
8-
Self::Instance(InstanceType { class })
8+
Self::NominalInstance(NominalInstanceType { class })
99
}
1010

11-
pub(crate) const fn into_instance(self) -> Option<InstanceType<'db>> {
11+
pub(crate) const fn into_instance(self) -> Option<NominalInstanceType<'db>> {
1212
match self {
13-
Type::Instance(instance_type) => Some(instance_type),
13+
Type::NominalInstance(instance_type) => Some(instance_type),
1414
_ => None,
1515
}
1616
}
1717
}
1818

1919
/// A type representing the set of runtime objects which are instances of a certain nominal class.
2020
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, salsa::Update)]
21-
pub struct InstanceType<'db> {
22-
// Keep this field private, so that the only way of constructing `InstanceType` instances
21+
pub struct NominalInstanceType<'db> {
22+
// Keep this field private, so that the only way of constructing `NominalInstanceType` instances
2323
// is through the `Type::instance` constructor function.
2424
class: ClassType<'db>,
2525
}
2626

27-
impl<'db> InstanceType<'db> {
27+
impl<'db> NominalInstanceType<'db> {
2828
pub(super) fn class(self) -> ClassType<'db> {
2929
self.class
3030
}
@@ -87,8 +87,8 @@ impl<'db> InstanceType<'db> {
8787
}
8888
}
8989

90-
impl<'db> From<InstanceType<'db>> for Type<'db> {
91-
fn from(value: InstanceType<'db>) -> Self {
92-
Self::Instance(value)
90+
impl<'db> From<NominalInstanceType<'db>> for Type<'db> {
91+
fn from(value: NominalInstanceType<'db>) -> Self {
92+
Self::NominalInstance(value)
9393
}
9494
}

crates/red_knot_python_semantic/src/types/known_instance.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! The `KnownInstance` type.
22
//!
3-
//! Despite its name, this is quite a different type from [`super::InstanceType`].
3+
//! Despite its name, this is quite a different type from [`super::NominalInstanceType`].
44
//! For the vast majority of instance-types in Python, we cannot say how many possible
55
//! inhabitants there are or could be of that type at runtime. Each variant of the
66
//! [`KnownInstanceType`] enum, however, represents a specific runtime symbol
@@ -249,7 +249,7 @@ impl<'db> KnownInstanceType<'db> {
249249
///
250250
/// For example, the symbol `typing.Literal` is an instance of `typing._SpecialForm`,
251251
/// so `KnownInstanceType::Literal.instance_fallback(db)`
252-
/// returns `Type::Instance(InstanceType { class: <typing._SpecialForm> })`.
252+
/// returns `Type::NominalInstance(NominalInstanceType { class: <typing._SpecialForm> })`.
253253
pub(super) fn instance_fallback(self, db: &dyn Db) -> Type {
254254
self.class().to_instance(db)
255255
}

crates/red_knot_python_semantic/src/types/subclass_of.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ impl<'db> SubclassOfType<'db> {
1616
/// This method does not always return a [`Type::SubclassOf`] variant.
1717
/// If the class object is known to be a final class,
1818
/// this method will return a [`Type::ClassLiteral`] variant; this is a more precise type.
19-
/// If the class object is `builtins.object`, `Type::Instance(<builtins.type>)` will be returned;
20-
/// this is no more precise, but it is exactly equivalent to `type[object]`.
19+
/// If the class object is `builtins.object`, `Type::NominalInstance(<builtins.type>)`
20+
/// will be returned; this is no more precise, but it is exactly equivalent to `type[object]`.
2121
///
2222
/// The eager normalization here means that we do not need to worry elsewhere about distinguishing
2323
/// between `@final` classes and other classes when dealing with [`Type::SubclassOf`] variants.

crates/red_knot_python_semantic/src/types/type_ordering.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,12 @@ pub(super) fn union_or_intersection_elements_ordering<'db>(
126126

127127
(Type::SubclassOf(_), _) => Ordering::Less,
128128
(_, Type::SubclassOf(_)) => Ordering::Greater,
129-
(Type::Instance(left), Type::Instance(right)) => left.class().cmp(&right.class()),
129+
(Type::NominalInstance(left), Type::NominalInstance(right)) => {
130+
left.class().cmp(&right.class())
131+
}
130132

131-
(Type::Instance(_), _) => Ordering::Less,
132-
(_, Type::Instance(_)) => Ordering::Greater,
133+
(Type::NominalInstance(_), _) => Ordering::Less,
134+
(_, Type::NominalInstance(_)) => Ordering::Greater,
133135

134136
(Type::TypeVar(left), Type::TypeVar(right)) => left.cmp(right),
135137
(Type::TypeVar(_), _) => Ordering::Less,

0 commit comments

Comments
 (0)