Skip to content

type.__dict__ #5957

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

Merged
merged 2 commits into from
Jul 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 12 additions & 0 deletions extra_tests/snippets/builtin_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,3 +595,15 @@ def my_repr_func():

# https://github.com/RustPython/RustPython/issues/3100
assert issubclass(types.BuiltinMethodType, types.BuiltinFunctionType)

assert type.__dict__["__dict__"].__objclass__ is type
assert (
type(type(type.__dict__["__dict__"]).__objclass__).__name__ == "member_descriptor"
)


class A(type):
pass


assert "__dict__" not in A.__dict__
33 changes: 19 additions & 14 deletions vm/src/builtins/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -936,9 +936,9 @@ impl Constructor for PyType {
return Err(vm.new_value_error("type name must not contain null characters"));
}

let (metatype, base, bases) = if bases.is_empty() {
let (metatype, base, bases, base_is_type) = if bases.is_empty() {
let base = vm.ctx.types.object_type.to_owned();
(metatype, base.clone(), vec![base])
(metatype, base.clone(), vec![base], false)
} else {
let bases = bases
.iter()
Expand Down Expand Up @@ -972,8 +972,9 @@ impl Constructor for PyType {
};

let base = best_base(&bases, vm)?;
let base_is_type = base.is(vm.ctx.types.type_type);

(metatype, base.to_owned(), bases)
(metatype, base.to_owned(), bases, base_is_type)
};

let qualname = dict
Expand Down Expand Up @@ -1021,17 +1022,21 @@ impl Constructor for PyType {
// All *classes* should have a dict. Exceptions are *instances* of
// classes that define __slots__ and instances of built-in classes
// (with exceptions, e.g function)
let __dict__ = identifier!(vm, __dict__);
attributes.entry(__dict__).or_insert_with(|| {
vm.ctx
.new_static_getset(
"__dict__",
vm.ctx.types.type_type,
subtype_get_dict,
subtype_set_dict,
)
.into()
});
// Also, type subclasses don't need their own __dict__ descriptor
// since they inherit it from type
if !base_is_type {
let __dict__ = identifier!(vm, __dict__);
attributes.entry(__dict__).or_insert_with(|| {
vm.ctx
.new_static_getset(
"__dict__",
vm.ctx.types.type_type,
subtype_get_dict,
subtype_set_dict,
)
.into()
});
}

// TODO: Flags is currently initialized with HAS_DICT. Should be
// updated when __slots__ are supported (toggling the flag off if
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy