|
1 | 1 | use crate::{
|
2 |
| - builtins::{PyStr, PyTuple, PyTupleRef, PyTypeRef}, |
| 2 | + builtins::{PyList, PyStr, PyTuple, PyTupleRef, PyTypeRef}, |
3 | 3 | common::hash,
|
| 4 | + function::IntoPyObject, |
4 | 5 | slots::{Hashable, SlotConstructor},
|
5 | 6 | IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
|
6 | 7 | TypeProtocol, VirtualMachine,
|
7 | 8 | };
|
8 | 9 | use std::fmt;
|
9 | 10 |
|
| 11 | +static ATTR_EXCEPTIONS: [&str; 8] = [ |
| 12 | + "__origin__", |
| 13 | + "__args__", |
| 14 | + "__parameters__", |
| 15 | + "__mro_entries__", |
| 16 | + "__reduce_ex__", // needed so we don't look up object.__reduce_ex__ |
| 17 | + "__reduce__", |
| 18 | + "__copy__", |
| 19 | + "__deepcopy__", |
| 20 | +]; |
| 21 | + |
10 | 22 | #[pyclass(module = "types", name = "GenericAlias")]
|
11 | 23 | pub struct PyGenericAlias {
|
12 | 24 | origin: PyTypeRef,
|
@@ -111,6 +123,17 @@ impl PyGenericAlias {
|
111 | 123 | fn origin(&self) -> PyObjectRef {
|
112 | 124 | self.origin.as_object().clone()
|
113 | 125 | }
|
| 126 | + |
| 127 | + #[pymethod(magic)] |
| 128 | + fn dir(&self, vm: &VirtualMachine) -> PyResult<PyList> { |
| 129 | + let dir = vm.dir(Some(self.origin()))?; |
| 130 | + for exc in ATTR_EXCEPTIONS.iter() { |
| 131 | + if !dir.contains((*exc).into_pyobject(vm), vm)? { |
| 132 | + dir.append((*exc).into_pyobject(vm)); |
| 133 | + } |
| 134 | + } |
| 135 | + Ok(dir) |
| 136 | + } |
114 | 137 | }
|
115 | 138 |
|
116 | 139 | fn is_typevar(obj: PyObjectRef) -> bool {
|
|
0 commit comments