Skip to content
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

A class variable that overrides an instance variable should still display as instance variable #692

Merged
merged 12 commits into from
Sep 10, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
17 changes: 17 additions & 0 deletions pydoctor/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,20 @@ class Attribute(Inheritable):
None value means the value is not initialized at the current point of the the process.
"""

def _inherits_instance_variable_kind(self) -> None:
tristanlatr marked this conversation as resolved.
Show resolved Hide resolved
"""
If any of the inherited members of a class variable is an instance variable,
then the subclass' class variable become an instance variable as well.
"""
if self.kind is not DocumentableKind.CLASS_VARIABLE:
return
docsources = self.docsources()
next(docsources)
for inherited in docsources:
if inherited.kind is DocumentableKind.INSTANCE_VARIABLE:
self.kind = DocumentableKind.INSTANCE_VARIABLE
break

# Work around the attributes of the same name within the System class.
_ModuleT = Module
_PackageT = Package
Expand Down Expand Up @@ -1451,6 +1465,9 @@ def postProcess(self) -> None:
if is_exception(cls):
cls.kind = DocumentableKind.EXCEPTION

for attrib in self.objectsOfType(Attribute):
attrib._inherits_instance_variable_kind()

for post_processor in self._post_processors:
post_processor(self)

Expand Down
72 changes: 71 additions & 1 deletion pydoctor/test/test_astbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -2395,4 +2395,74 @@ def __init__(self):
'''

mod = fromText(src, systemcls=systemcls)
assert getConstructorsText(mod.contents['Animal']) == "Animal()"
assert getConstructorsText(mod.contents['Animal']) == "Animal()"

@systemcls_param
def test_class_var_override(systemcls: Type[model.System]) -> None:

src = '''\
from number import Number
class Thing(object):
def __init__(self):
self.var: Number = 1
class Stuff(Thing):
var:float
'''

mod = fromText(src, systemcls=systemcls, modname='mod')
var = mod.system.allobjects['mod.Stuff.var']
assert var.kind == model.DocumentableKind.INSTANCE_VARIABLE

@systemcls_param
def test_class_var_override_traverse_subclasses(systemcls: Type[model.System]) -> None:

src = '''\
from number import Number
class Thing(object):
def __init__(self):
self.var: Number = 1
class _Stuff(Thing):
...
class Stuff(_Stuff):
var:float
'''

mod = fromText(src, systemcls=systemcls, modname='mod')
var = mod.system.allobjects['mod.Stuff.var']
assert var.kind == model.DocumentableKind.INSTANCE_VARIABLE

src = '''\
from number import Number
class Thing(object):
def __init__(self):
self.var: Optional[Number] = 0
class _Stuff(Thing):
var = None
class Stuff(_Stuff):
var: float
'''

mod = fromText(src, systemcls=systemcls, modname='mod')
var = mod.system.allobjects['mod._Stuff.var']
assert var.kind == model.DocumentableKind.INSTANCE_VARIABLE

mod = fromText(src, systemcls=systemcls, modname='mod')
var = mod.system.allobjects['mod.Stuff.var']
assert var.kind == model.DocumentableKind.INSTANCE_VARIABLE

def test_class_var_override_attrs() -> None:

systemcls = AttrsSystem

src = '''\
import attr
@attr.s
class Thing(object):
var = attr.ib()
class Stuff(Thing):
var: float
'''

mod = fromText(src, systemcls=systemcls, modname='mod')
var = mod.system.allobjects['mod.Stuff.var']
assert var.kind == model.DocumentableKind.INSTANCE_VARIABLE