-
Notifications
You must be signed in to change notification settings - Fork 62
Handling of attrs and dataclass constructors #656
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
Open
tristanlatr
wants to merge
76
commits into
master
Choose a base branch
from
305-handling-of-constructors
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
76 commits
Select commit
Hold shift + click to select a range
bf5716a
Add test
tristanlatr f7d0d1a
Add src
tristanlatr d87e745
Structure tests
tristanlatr f022cdf
wip Class.constructors attribute
tristanlatr d578d4c
Add extra information with constructor signature that links to the de…
tristanlatr 6f8e806
Introduce the SignatureBuilder class and refactor the astbuilder to u…
tristanlatr 24d5ad6
wip
tristanlatr 379c63e
Fix issues
tristanlatr a527d97
Merge branch 'master' into 305-handling-of-constructors
tristanlatr c5fc9b9
try to fix mypy
tristanlatr c4be1a9
Merge branch 'master' into 305-handling-of-constructors
tristanlatr a2389dc
Abstract out some of the core visiting code for dataclass like classes
tristanlatr 36e3683
Factor-out callable analysis inside attrs.py into functions in astuti…
tristanlatr d974015
Fix docstrings
tristanlatr d1281cf
Update pydoctor/astutils.py
tristanlatr 41d8ddb
Refactors
tristanlatr bb7c62c
Merge branch '718-dataclass-like-abstraction' of github.com:twisted/p…
tristanlatr ba47bab
Fix docstring
tristanlatr 1f74226
Merge branch '718-dataclass-like-abstraction' into 305-handling-of-co…
tristanlatr 9b3860b
remove commented code
tristanlatr a3f0cd3
Remove unused imports
tristanlatr 4822ea9
Fix annotation of signature_from_functiondef()
tristanlatr 3f8c7f6
Improve support for attrs generated classes, still WIP...
tristanlatr 31ef268
Better understand the factory parameter.
tristanlatr c06de7e
Fix detected regression in overload handling
tristanlatr 2155fec
Improve annotation_from_attrib()
tristanlatr 4b9142e
Better handle multiple dataclass like extensions
tristanlatr cf8ba4e
Merge branch '718-dataclass-like-abstraction' into 305-handling-of-co…
tristanlatr f7220e9
Adjust postProcess
tristanlatr def0ee0
refarctors and add test
tristanlatr 5a1939c
Add support for auto_detect parameter
tristanlatr 411a584
Support inherited constructor params
tristanlatr 5c72350
Fix presentation of constructors of attrs class
tristanlatr 3720604
Fix order of arguments in constructor short text
tristanlatr 299cd58
Fix keyword only feature
tristanlatr 7497b7a
Add support for the new APIs of attrs, fixes #718
tristanlatr 6a33634
Fix/silent mypy warnings and other refactors
tristanlatr 4ce9b97
Add a docstring to attrs generated __init__ methods
tristanlatr 4016b4c
Fix bugs regarding the rendering of constructors
tristanlatr eb83341
docs
tristanlatr 06e06e1
Merge branch 'master' into 305-handling-of-constructors
tristanlatr 63edbed
Add documentation section on improved attrs support
tristanlatr 1e82784
Add tests
tristanlatr 435ac99
Merge branch '305-handling-of-constructors' of github.com:twisted/pyd…
tristanlatr bcce4f3
Fix little issue of priorization of presented annotations
tristanlatr ad9bfef
Fix mypy
tristanlatr 607a89a
add changelog entry
tristanlatr 3e512f4
make it pass the tests on older versions of python as well.
tristanlatr 11ba98c
normal indentation
tristanlatr 83adf80
skip type comment test on python < 3.8
tristanlatr ce1e05f
minor changes
tristanlatr 34d7bb3
remove unused imports
tristanlatr fe08559
Merge branch 'master' into 305-handling-of-constructors
tristanlatr 89a58ef
Merge branch 'master' into 305-handling-of-constructors
tristanlatr d4deb77
Fix merge error
tristanlatr d81bf62
Merge branch 'master' into 305-handling-of-constructors
tristanlatr 45a5d3a
Merge branch 'master' into 305-handling-of-constructors
tristanlatr 4926d47
Add comments
tristanlatr 2ac31e7
Merge branch '305-handling-of-constructors' of github.com:twisted/pyd…
tristanlatr b21ad7e
Merge branch 'master' into 305-handling-of-constructors
tristanlatr 417e995
Merge branch 'master' into 305-handling-of-constructors
tristanlatr a1fff97
WIP- merging dataclass, attrs and other utilities into a single module.
tristanlatr 60c319e
Merge branch '305-handling-of-constructors' of github.com:twisted/pyd…
tristanlatr b59a6c0
.source_code is not an attribute
tristanlatr a62e2f2
wip...
tristanlatr bd658dc
Still work in progress... 12 dataclass related tests failed, all test…
tristanlatr da74a74
Merge branch 'master' into 305-handling-of-constructors
tristanlatr 537ecf7
Fix all tests
tristanlatr 7ad8099
Use different link for attrs and dataclasses
tristanlatr b79a7d7
Fix mypy
tristanlatr 88ff3ed
Merge branch 'master' into 305-handling-of-constructors
tristanlatr dcc5e07
Fix mypy and remove commented code
tristanlatr 049b417
Move the changelog entries to the development section
tristanlatr 6a3c870
Remove unused imports
tristanlatr c15fb73
Cleanup import
tristanlatr faed763
Merge branch 'master' into 305-handling-of-constructors
tristanlatr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -26,8 +26,7 @@ Pydoctor also supports *attribute docstrings*:: | |||||
| """This docstring describes a class variable.""" | ||||||
|
|
||||||
| def __init__(self): | ||||||
| self.ivar = [] | ||||||
| """This docstring describes an instance variable.""" | ||||||
| self.ivar = [];"It can also be used inline." | ||||||
|
|
||||||
| Attribute docstrings are not part of the Python language itself (`PEP 224 <https://www.python.org/dev/peps/pep-0224/>`_ was rejected), so these docstrings are not available at runtime. | ||||||
|
|
||||||
|
|
@@ -284,6 +283,45 @@ If you are using explicit ``attr.ib`` definitions instead of ``auto_attribs``, p | |||||
| list_of_numbers = attr.ib(factory=list) # type: List[int] | ||||||
| """Multiple numbers.""" | ||||||
|
|
||||||
| Pydoctor look for ``attrs`` fields declarations and analyze the | ||||||
| arguments passed to ``attr.s`` and ``attr.ib`` in order to | ||||||
| precisely infer what's the signature of the constructor method:: | ||||||
|
|
||||||
| from typing import List | ||||||
| import pathlib | ||||||
| import attr | ||||||
|
|
||||||
| def convert_paths(p:List[str]) -> List[pathlib.Path]: | ||||||
| return [pathlib.Path(s) for s in p] | ||||||
|
|
||||||
| @attr.s(auto_attribs=True) | ||||||
| class Base: | ||||||
| a: int | ||||||
|
|
||||||
| @attr.s(auto_attribs=True, kw_only=True) | ||||||
| class SomeClass(Base): | ||||||
| a_number:int=42; "docstring of number A." | ||||||
| list_of_numbers:List[int] = attr.ib(factory=list); "List of ints" | ||||||
| converted_paths:List[pathlib.Path] = attr.ib(converter=convert_paths, factory=list); "Uses a converter" | ||||||
|
|
||||||
| The constrcutor method will be documented as if it was explicitly defined, | ||||||
| with a docstring including documentation of each parameters and a note | ||||||
| saying the method is generated by attrs:: | ||||||
|
|
||||||
| def __init__(self, *, a: int, a_number: int = 42, | ||||||
| list_of_numbers: List[int] = list(), | ||||||
| converted_paths: List[str] = list()): | ||||||
| """ | ||||||
| attrs generated method | ||||||
|
|
||||||
| @param a_number: docstring of number A. | ||||||
| @param list_of_numbers: C{attr.ib(factory=list)} | ||||||
| List of ints | ||||||
| @param converted_paths: C{attr.ib(converter=convert_paths, factory=list)} | ||||||
| Uses a converter | ||||||
| """ | ||||||
|
|
||||||
| Pydoctor also supports the newer APIs (``attrs.define``/``attrs.field``). | ||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| Private API | ||||||
| ----------- | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be a note like
@note:, with a title line "Initialize a SomeClass"? In the summary, I think seeing "attrs generated method" on every__init__is even less helpful than that.