Open jeremybettis opened 12 months ago
Interesting one @jeremybettis and thanks for the report. Perhaps we could simplify further and remove the linecache part altogether:
--- a/pylint/extensions/docstyle.py
+++ b/pylint/extensions/docstyle.py
@@ -60,12 +60,15 @@ class DocStringStyleChecker(checkers.BaseChecker):
confidence=HIGH,
)
- # Use "linecache", instead of node.as_string(), because the latter
- # looses the original form of the docstrings.
-
+ # Use this approach instead of node.as_string(), because the latter
+ # loses the original form of the docstrings.
if docstring:
- lineno = node.fromlineno + 1
- line = linecache.getline(node.root().file, lineno).lstrip()
+ with node.root().stream() as stream:
+ for number, line in enumerate(stream):
+ if number == node.fromlineno:
+ line = line.decode().lstrip()
+ break
+
if line and line.find('"""') == 0:
return
if line and "'''" in line:
Bug description
When using --from-stdin, the docstyle plugin still reads the file directly. If the line where the doc string is in stdin happens to match with another string in the file on disk, then it will give an error about an invalid docstring quote.
In a situation where you might be running pylint from a git commit that is not the current working copy, this could be very common.
This patch would fix the issue, but it's not very efficient, doesn't check for exceptions, etc.
Configuration
No response
Command used
Create 2 files
hello.py
hello_modified.py
Run this command:
cat hello_modified.py | pylint --from-stdin --load-plugins=pylint.extensions.docstyle hello.py
Pylint output
Expected behavior
I expected that there would be no errors, just as if running pylint directly on hello_modified.py
Pylint version
OS / Environment
Debian GNU/Linux rodete 20230901.00.02RD
Additional dependencies
No response