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

Handling of attrs/dataclass/pydantic/namedtuple constructors #656

Draft
wants to merge 63 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
bf5716a
Add test
tristanlatr Sep 17, 2022
f7d0d1a
Add src
tristanlatr Sep 17, 2022
d87e745
Structure tests
tristanlatr Sep 18, 2022
f022cdf
wip Class.constructors attribute
tristanlatr Sep 18, 2022
d578d4c
Add extra information with constructor signature that links to the de…
tristanlatr Sep 19, 2022
6f8e806
Introduce the SignatureBuilder class and refactor the astbuilder to u…
tristanlatr Sep 19, 2022
24d5ad6
wip
tristanlatr Sep 27, 2022
379c63e
Fix issues
tristanlatr Oct 31, 2022
a527d97
Merge branch 'master' into 305-handling-of-constructors
tristanlatr Oct 31, 2022
c5fc9b9
try to fix mypy
tristanlatr Oct 31, 2022
c4be1a9
Merge branch 'master' into 305-handling-of-constructors
tristanlatr Jun 5, 2023
a2389dc
Abstract out some of the core visiting code for dataclass like classes
tristanlatr Jun 11, 2023
36e3683
Factor-out callable analysis inside attrs.py into functions in astuti…
tristanlatr Jun 11, 2023
d974015
Fix docstrings
tristanlatr Jun 11, 2023
d1281cf
Update pydoctor/astutils.py
tristanlatr Jun 11, 2023
41d8ddb
Refactors
tristanlatr Jun 11, 2023
bb7c62c
Merge branch '718-dataclass-like-abstraction' of github.com:twisted/p…
tristanlatr Jun 11, 2023
ba47bab
Fix docstring
tristanlatr Jun 11, 2023
1f74226
Merge branch '718-dataclass-like-abstraction' into 305-handling-of-co…
tristanlatr Jun 11, 2023
9b3860b
remove commented code
tristanlatr Jun 11, 2023
a3f0cd3
Remove unused imports
tristanlatr Jun 11, 2023
4822ea9
Fix annotation of signature_from_functiondef()
tristanlatr Jun 11, 2023
3f8c7f6
Improve support for attrs generated classes, still WIP...
tristanlatr Jun 14, 2023
31ef268
Better understand the factory parameter.
tristanlatr Jun 16, 2023
c06de7e
Fix detected regression in overload handling
tristanlatr Jul 6, 2023
2155fec
Improve annotation_from_attrib()
tristanlatr Jul 7, 2023
4b9142e
Better handle multiple dataclass like extensions
tristanlatr Jul 7, 2023
cf8ba4e
Merge branch '718-dataclass-like-abstraction' into 305-handling-of-co…
tristanlatr Jul 7, 2023
f7220e9
Adjust postProcess
tristanlatr Jul 7, 2023
def0ee0
refarctors and add test
tristanlatr Jul 7, 2023
5a1939c
Add support for auto_detect parameter
tristanlatr Jul 7, 2023
411a584
Support inherited constructor params
tristanlatr Jul 7, 2023
5c72350
Fix presentation of constructors of attrs class
tristanlatr Jul 7, 2023
3720604
Fix order of arguments in constructor short text
tristanlatr Jul 7, 2023
299cd58
Fix keyword only feature
tristanlatr Jul 8, 2023
7497b7a
Add support for the new APIs of attrs, fixes #718
tristanlatr Jul 8, 2023
6a33634
Fix/silent mypy warnings and other refactors
tristanlatr Jul 9, 2023
4ce9b97
Add a docstring to attrs generated __init__ methods
tristanlatr Jul 9, 2023
4016b4c
Fix bugs regarding the rendering of constructors
tristanlatr Jul 9, 2023
eb83341
docs
tristanlatr Jul 9, 2023
06e06e1
Merge branch 'master' into 305-handling-of-constructors
tristanlatr Jul 9, 2023
63edbed
Add documentation section on improved attrs support
tristanlatr Jul 9, 2023
1e82784
Add tests
tristanlatr Jul 9, 2023
435ac99
Merge branch '305-handling-of-constructors' of github.com:twisted/pyd…
tristanlatr Jul 10, 2023
bcce4f3
Fix little issue of priorization of presented annotations
tristanlatr Jul 10, 2023
ad9bfef
Fix mypy
tristanlatr Jul 14, 2023
607a89a
add changelog entry
tristanlatr Jul 14, 2023
3e512f4
make it pass the tests on older versions of python as well.
tristanlatr Jul 14, 2023
11ba98c
normal indentation
tristanlatr Jul 14, 2023
83adf80
skip type comment test on python < 3.8
tristanlatr Jul 14, 2023
ce1e05f
minor changes
tristanlatr Jul 14, 2023
34d7bb3
remove unused imports
tristanlatr Jul 18, 2023
fe08559
Merge branch 'master' into 305-handling-of-constructors
tristanlatr Sep 28, 2023
89a58ef
Merge branch 'master' into 305-handling-of-constructors
tristanlatr Sep 28, 2023
d4deb77
Fix merge error
tristanlatr Nov 3, 2023
d81bf62
Merge branch 'master' into 305-handling-of-constructors
tristanlatr Nov 3, 2023
45a5d3a
Merge branch 'master' into 305-handling-of-constructors
tristanlatr Dec 7, 2023
4926d47
Add comments
tristanlatr Jan 3, 2024
2ac31e7
Merge branch '305-handling-of-constructors' of github.com:twisted/pyd…
tristanlatr Jan 3, 2024
b21ad7e
Merge branch 'master' into 305-handling-of-constructors
tristanlatr Jan 17, 2024
417e995
Merge branch 'master' into 305-handling-of-constructors
tristanlatr Feb 10, 2024
a1fff97
WIP- merging dataclass, attrs and other utilities into a single module.
tristanlatr Mar 24, 2024
60c319e
Merge branch '305-handling-of-constructors' of github.com:twisted/pyd…
tristanlatr Mar 24, 2024
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
Prev Previous commit
Next Next commit
Structure tests
  • Loading branch information
tristanlatr committed Sep 18, 2022
commit d87e7459ed6505c85b67f29a00d3cfda3ea470e0
98 changes: 88 additions & 10 deletions pydoctor/test/test_astbuilder.py
Original file line number Diff line number Diff line change
@@ -2116,28 +2116,106 @@ def test_prepend_package_real_path(systemcls: Type[model.System]) -> None:
systemcls.systemBuilder = _builderT_init

@systemcls_param
def test_constructor_signature_init_and_new(systemcls: Type[model.System]) -> None:
"""
Pydoctor can infer the constructor signature when both __new__ and __init__ are
"""
def test_constructor_signature_init(systemcls: Type[model.System]) -> None:

src = '''\
class Person(object):
# pydoctor can infer the constructor to be: "Person(name, age)"
def __init__(self, name, age):
self.name = name
self.age = age

class Citizen(Person):
# pydoctor can infer the constructor to be: "Citizen(nationality, *args, **kwargs)"
def __init__(self, nationality, *args, **kwargs):
self.nationality = nationality
super(Citizen, self).__init__(*args, **kwargs)
'''

# assert that Person.extra_info contains the available constructors names
# Like "Available constructor: ``Person(name, age)``" that links to Person.__init__ documentation.

# assert that Citizen.extra_info contains the available constructors names
# Like "Available constructor: ``Citizen(nationality, *args, **kwargs)``" that links to Citizen.__init__ documentation.

src1 = '''\
@systemcls_param
def test_constructor_signature_new(systemcls: Type[model.System]) -> None:
src = '''\
class Animal(object):
# pydoctor can infer the constructor to be: "Animal(name)"
def __new__(cls, name):
print('__new__() called.')
obj = super().__new__(cls)
obj.name = name # not recognized by pydoctor
# assignation not recognized by pydoctor, attribute 'name' will not be documented
obj.name = name
return obj
'''

src2 = '''\
@systemcls_param
def test_constructor_signature_init_and_new(systemcls: Type[model.System]) -> None:
"""
Pydoctor can't infer the constructor signature when both __new__ and __init__ are defined.
__new__ takes the precedence over __init__ because it's called first. Trying to infer what are the complete
constructor signature when __new__ is defined might be very hard because the method can return an instance of
another class, calling another __init__ method. We're not there yet in term of static analysis.
"""

src = '''\
class Animal(object):
# Can be omitted, Python will give a default implementation
# both __init__ and __new__ are defined, pydoctor only looks at the __new__ method
# pydoctor infers the constructor to be: "Animal(*args, **kw)"
def __new__(cls, *args, **kw):
print('__new__() called.')
print('args: ', args, ', kw: ', kw)
return super().__new__(cls)

def __init__(self, name):
print('__init__() called.')
self.name = name'''
self.name = name

class Cat(Animal):
# Idem, but __new__ is inherited.
# pydoctor infers the constructor to be: "Cat(*args, **kw)"
# This is why it's important to still document __init__ as a regular method.
def __init__(self, name, owner):
super().__init__(name)
self.owner = owner
'''

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

src = '''\

def get_default_options() -> 'Options':
"""
This is another constructor for class 'Options'.
But it's not recognized by pydoctor at this moment.
"""
return Options()

class Options:
a,b,c = None, None, None

@classmethod
def create_no_hints(cls):
"""
Pydoctor can't deduce that this method is a constructor as well,
because there is no type annotation.
"""
return cls()

# thanks to type hints,
# pydoctor can infer the constructor to be: "Options.create()"
@staticmethod
def create() -> 'Options':
# the fictional constructor is not detected by pydoctor, because it doesn't exists actually.
return Options(1,2,3)

# thanks to type hints,
# pydoctor can infer the constructor to be: "Options.create_from_num(num)"
@classmethod
def create_from_num(cls, num) -> 'Options':
c = cls.create()
c.a = num
return c
'''