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

Add doc_node attribute #1276

Merged
merged 64 commits into from
Feb 28, 2022
Merged

Add doc_node attribute #1276

merged 64 commits into from
Feb 28, 2022

Conversation

kasium
Copy link
Contributor

@kasium kasium commented Nov 26, 2021

Steps

  • For new features or bug fixes, add a ChangeLog entry describing what your PR does.
  • Write a good description on what the PR does.

Description

This change adds a new attribute doc_node to ClassDef, FunctionDef, AsyncFunctionDef and Module. This is needed, since some checks in pylint not only need the docstring as a string, but the node containing it. For details see pylint-dev/pylint#3077

Type of Changes

Type
✨ New feature

Related Issue

pylint-dev/pylint#3077

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this, I expected more work to be fair, this is rather elegant. As it's a "big" features I'll wait for opinion on it and it's going to be in astroid 2.10.

tests/unittest_nodes.py Outdated Show resolved Hide resolved
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.10.0 milestone Nov 27, 2021
@Pierre-Sassoulas Pierre-Sassoulas added the Enhancement ✨ Improvement to a component label Nov 27, 2021
@DanielNoord
Copy link
Collaborator

I haven't reviewed the code yet, but can't this be added to all nodes?

VARIABLE = 1
"""A variable I will use somewhere"""

IDEs know that this docstring belongs to the variable. If we're creating this new attribute we might as well try and do it for all nodes? This would allows allow us to call NodeNG.doc_node instead of having to do an isinstance first.

@DanielNoord DanielNoord self-requested a review November 27, 2021 22:40
@kasium
Copy link
Contributor Author

kasium commented Nov 29, 2021

I haven't reviewed the code yet, but can't this be added to all nodes?

VARIABLE = 1
"""A variable I will use somewhere"""

IDEs know that this docstring belongs to the variable. If we're creating this new attribute we might as well try and do it for all nodes? This would allows allow us to call NodeNG.doc_node instead of having to do an isinstance first.

To doc_node is only used for four nodes, so does it really make sense to add it to all. Especially if doc is also just present for four nodes?

@DanielNoord
Copy link
Collaborator

To doc_node is only used for four nodes, so does it really make sense to add it to all. Especially if doc is also just present for four nodes?

As I said, one of the benefits would be that we can do node.doc_node without first inferring the type of node without running into issues. This is quite a nice benefit!
A second thing is that we never know when we might need this information. Perhaps somebody would like to make a highly opinionated checker that forces all global constants to have a docstring? We might as well implement this in its fulllest and most productive form 😄

By the way, I'm happy to assist in changing this so it is added to all node types! Just let me know if I can help!

@kasium
Copy link
Contributor Author

kasium commented Nov 29, 2021

@DanielNoord okay, the let me add it to all nodes. Which is the right class for that?

@DanielNoord
Copy link
Collaborator

@DanielNoord okay, the let me add it to all nodes. Which is the right class for that?

NodeNG is the base class used by all other nodes. That would be the first place to start looking.

@kasium
Copy link
Contributor Author

kasium commented Nov 30, 2021

@DanielNoord okay, the let me add it to all nodes. Which is the right class for that?

NodeNG is the base class used by all other nodes. That would be the first place to start looking.

done

Copy link
Collaborator

@DanielNoord DanielNoord left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for taking a little longer to review this!

Although this wouldn't actually add doc_node to Const nodes, I think it is a good start. Adding it as an attribute to NodeNG makes sense to me anyway.

Please let me know if you have any questions! Happy to help move this along 😄

.gitignore Outdated Show resolved Hide resolved
astroid/nodes/node_ng.py Outdated Show resolved Hide resolved
astroid/nodes/scoped_nodes.py Outdated Show resolved Hide resolved
tests/unittest_nodes.py Outdated Show resolved Hide resolved
tests/unittest_nodes.py Show resolved Hide resolved
astroid/rebuilder.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@DanielNoord DanielNoord left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cdce8p Not sure if this was ready for review yet, but I thought it would be good to keep up with what you're doing so we can merge this as soon as it is done. Please dismiss if this is not ready yet.
I only have minor questions and comments though.

Could you also add a test case for return annotation in string format for a function definition? From my initial read of the code I wondered if this would be handled correctly. I tested, and it does (why would I think otherwise with one of your PR's 😅) but it would be good to have a test case as well.

astroid/rebuilder.py Outdated Show resolved Hide resolved
astroid/rebuilder.py Show resolved Hide resolved
astroid/rebuilder.py Outdated Show resolved Hide resolved
astroid/rebuilder.py Show resolved Hide resolved
ChangeLog Outdated Show resolved Hide resolved
@cdce8p
Copy link
Member

cdce8p commented Feb 28, 2022

@kasium Sorry for taking over your PR. You already did an awesome job 🚀 Unfortunately, this PR blocks the next pylint release. Combined with the necessary changes, I though that it would be faster to work on it myself.

@cdce8p Not sure if this was ready for review yet, but I thought it would be good to keep up with what you're doing so we can merge this as soon as it is done. Please dismiss if this is not ready yet. I only have minor questions and comments though.

It is ready now 😄

Could you also add a test case for return annotation in string format for a function definition? From my initial read of the code I wondered if this would be handled correctly. I tested, and it does (why would I think otherwise with one of your PR's 😅) but it would be good to have a test case as well.

Done. Although string return annotations weren't an issue, I only accepted comments for Module docstrings. Noticed that this might be useful as I was writing the test case. It's fixed now.

@cdce8p cdce8p changed the title Add doc-node attribute Add doc_node attribute Feb 28, 2022
@cdce8p cdce8p removed the Blocked 🚧 A PR or issue blocked by another PR or issue label Feb 28, 2022
@cdce8p cdce8p modified the milestones: 2.12.0, 2.11.0 Feb 28, 2022
@DanielNoord DanielNoord self-requested a review February 28, 2022 22:13
data = "\n".join(self._data[lineno - 1 : end_range])

found_start, found_end = False, False
open_brackets = 0
skip_token: set[int] = {token.NEWLINE, token.INDENT}
skip_token: Set[int] = {token.NEWLINE, token.INDENT}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually saw this during review and thought I had learned something new from you (set being able to used on < 3.8/9) but alas... 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can actually be used be used in 3.7 and 3.8 (with from __future__ import annotations), but not in 3.6.
Really time to remove support for 3.6 soon.

Technically, you could use "set[int]" (note the quotation marks) in 3.6, but that isn't really worth it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I have been looking forward to seeing you use your typing updater tool 😄

@cdce8p cdce8p mentioned this pull request Feb 28, 2022
Copy link
Collaborator

@DanielNoord DanielNoord left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't need a test for the order issue as it will be found in #1411

@cdce8p
Copy link
Member

cdce8p commented Feb 28, 2022

I think we don't need a test for the order issue as it will be found in #1411

Sounds good!

Have you been able to test the pylint issue? Will this PR resolve it?

@DanielNoord
Copy link
Collaborator

DanielNoord commented Feb 28, 2022

I think we don't need a test for the order issue as it will be found in #1411

Sounds good!

Have you been able to test the pylint issue? Will this PR resolve it?

Not tested, but I'm fairly positive it should. See my comment in #1411. If those tests pass pylint will pass as well.
I'll do a final check just to be sure. Give me a minute.

Edit:
@cdce8p Fixed 😄

@cdce8p
Copy link
Member

cdce8p commented Feb 28, 2022

Edit: @cdce8p Fixed 😄

I'll do one last check against Home Assistant, just to make sure and merge it afterwards. That way we can both still work on the two followups today and hopefully have the release ready tomorrow.

Around ~10min.

@cdce8p cdce8p dismissed their stale review February 28, 2022 22:40

Updated files

@DanielNoord
Copy link
Collaborator

Edit: @cdce8p Fixed 😄

I'll do one last check against Home Assistant, just to make sure and merge it afterwards. That way we can both still work on the two followups today and hopefully have the release ready tomorrow.

Around ~10min.

@cdce8p Sounds good to me! Ping me when you need me 😄

My PR is ready. You can review it if you want. It's just a bunch of tests now.

@cdce8p cdce8p merged commit c0d2e5c into pylint-dev:main Feb 28, 2022
@kasium kasium deleted the issue-3077 branch March 1, 2022 10:59
@kasium
Copy link
Contributor Author

kasium commented Mar 1, 2022

Thanks for finishing it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement ✨ Improvement to a component
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Impossible to get correct line number for the docstrings
5 participants