Compare commits

..

2 Commits

Author SHA1 Message Date
Felföldy Tibor
a156aaa81c Update dir tests 2025-01-26 23:18:44 +01:00
Felföldy Tibor
a7d83934c1 Add dir __base__ recursively; Add dir tests to 70_builtins.py 2025-01-26 21:45:00 +01:00
2 changed files with 25 additions and 9 deletions

View File

@ -85,18 +85,18 @@ def dir(obj=None):
if obj is None:
return list(globals().keys())
if hasattr(obj, "__dir__"):
return obj.__dir__()
attributes = set()
# Set object attributes.
if not isinstance(obj, type):
if hasattr(obj, "__dir__"):
return sorted(obj.__dir__())
attributes.update(dir(type(obj)))
if hasattr(obj, "__dict__"):
attributes.update(obj.__dict__.keys())
# Set type attributes.
if not isinstance(obj, type):
if hasattr(type(obj), "__dict__"):
attributes.update(type(obj).__dict__.keys())
if hasattr(obj, "__base__") and obj.__base__ is not None:
attributes.update(dir(obj.__base__))
return sorted(attributes)

View File

@ -49,4 +49,20 @@ assert not all([True, False])
assert not all([False, False])
assert list(enumerate([1,2,3])) == [(0,1), (1,2), (2,3)]
assert list(enumerate([1,2,3], 1)) == [(1,1), (2,2), (3,3)]
assert list(enumerate([1,2,3], 1)) == [(1,1), (2,2), (3,3)]
assert "__name__" in dir()
class Base:
def inherited(): ...
class Test(Base):
cls_attr = 'a'
def __init__(self):
self.self_attr = 1
assert {"self_attr", "cls_attr", "inherited"}.issubset(dir(Test()))
class CustomDir:
def __dir__(self):
return ["custom"]
assert ["custom"] == dir(CustomDir())