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

522 pre commit compatibillity issue with pythonpath #530

Merged
Show file tree
Hide file tree
Changes from all 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
36 changes: 30 additions & 6 deletions docsig/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from __future__ import annotations as _

import logging as _logging
import os as _os
import sys as _sys
from pathlib import Path as _Path

Expand Down Expand Up @@ -141,10 +142,14 @@ def _from_file(
check_class_constructor,
) -> _Parent:
try:
string = path.read_text(encoding="utf-8")
code = path.read_text(encoding="utf-8")
parent = _from_str(
context={
"code": code,
"module_name": derive_module_name(path),
"path": path,
},
messages=messages,
string=string,
path=path,
ignore_args=ignore_args,
ignore_kwargs=ignore_kwargs,
Expand All @@ -162,7 +167,7 @@ def _from_file(


def _from_str( # pylint: disable=too-many-arguments
string: str,
context: dict,
messages: _Messages,
ignore_args: bool,
ignore_kwargs: bool,
Expand All @@ -172,8 +177,8 @@ def _from_str( # pylint: disable=too-many-arguments
logger = _logging.getLogger(__package__)
try:
parent = _Parent(
_ast.parse(string),
_Directives.from_text(string, messages),
_ast.parse(**context),
_Directives.from_text(context["code"], messages),
path,
ignore_args,
ignore_kwargs,
Expand Down Expand Up @@ -427,7 +432,9 @@ def docsig( # pylint: disable=too-many-locals,too-many-arguments
return max(retcodes)

module = _from_str(
string,
{
"code": string,
},
disable or _Messages(),
ignore_args,
ignore_kwargs,
Expand All @@ -449,3 +456,20 @@ def docsig( # pylint: disable=too-many-locals,too-many-arguments
target or _Messages(),
)
return _report(failures, no_ansi=no_ansi)


def derive_module_name(
file_path: str | _Path,
) -> str:
"""Extract Python module names from paths.

A dot separated Python module name is generated from the given file
path.

:param file_path: A file system path.
:returns: The converted Python module name.
"""
converted = _os.path.splitext(str(file_path))[0]
converted = converted.replace(_os.sep, ".")
converted = converted.replace("-", "_")
return converted
8 changes: 7 additions & 1 deletion docsig/_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import typing as _t

from astroid.nodes.scoped_nodes import scoped_nodes as _scoped_nodes

from ._module import Error as _Error
from ._module import Function as _Function
from ._stub import UNNAMED as _UNNAMED
Expand Down Expand Up @@ -57,7 +59,11 @@ def __init__(
self._func.messages.extend(i for i in _E.all if i not in target)

self._name = self._func.name
if self._func.parent is not None and self._func.parent.name:
if (
self._func.parent is not None
and self._func.parent.name
and not isinstance(self._func.parent, _scoped_nodes.Module)
):
self._name = f"{self._func.parent.name}.{self._func.name}"

self._check_property_returns = check_property_returns
Expand Down
10 changes: 7 additions & 3 deletions tests/_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,8 +572,12 @@ def test_string_argument(
:param template: String data.
:param expected: Expected output.
"""
assert main(*CHECK_ARGS, long.string, template, test_flake8=False) == int(
name.startswith(FAIL)
)
assert main(
"mocked_path",
*CHECK_ARGS,
long.string,
template,
test_flake8=False,
) == int(name.startswith(FAIL))
std = capsys.readouterr()
assert expected in std.out
69 changes: 53 additions & 16 deletions tests/benchmark_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@


@pytest.mark.parametrize(
"template",
"template,path",
[
'''
(
'''
class Klass:
def __init__(self, param1, param2) -> None:
"""Info about class.
Expand All @@ -22,7 +23,10 @@ def __init__(self, param1, param2) -> None:
:param param1: Info about param1.
"""
''',
'''
"klass.py",
),
(
'''
class Klass:
"""Info about class.

Expand All @@ -33,7 +37,10 @@ class Klass:
def __init__(self, param1, param2) -> None:
pass
''',
'''
"klass.py",
),
(
'''
class Klass:
def __get__(self, param1, param2) -> None:
"""Info about class.
Expand All @@ -42,7 +49,10 @@ def __get__(self, param1, param2) -> None:
:param param1: Info about param1.
"""
''',
'''
"klass.py",
),
(
'''
def my_function(argument: int = 42) -> int:
"""
Function that prints a message and returns the argument + 1
Expand All @@ -67,7 +77,10 @@ def my_external_function(argument: int = 42) -> int:

return argument + 1
''',
'''
"my_function.py",
),
(
'''
class Parent:
def method(self) -> None:
"""This is documented."""
Expand All @@ -77,25 +90,37 @@ class Child(Parent):
def method(self) -> None:
self._set: _t.Set[T] = set()
''',
'''
"parent_and_child.py",
),
(
'''
class Klass:
@property
def prop(self) -> str:
"""This is documented."""
''',
'''
"klass_with_property.py",
),
(
'''
class _Klass:
@property
def prop(self, param1) -> str:
"""This is documented."""
''',
'''
"klass_with_documented_property.py",
),
(
'''
class Parent:
def _protected(self, param1) -> None:
"""This is documented."""
self._set: _t.Set[T] = set()
''',
'''
"parent_with_protected.py",
),
(
'''
def function(param1, param2, *args) -> None:
"""Proper docstring.

Expand All @@ -104,7 +129,10 @@ def function(param1, param2, *args) -> None:
:param args: Pass
"""
''',
'''
"function_with_args.py",
),
(
'''
def function(param1, param2, **kwargs) -> None:
"""Proper docstring.

Expand All @@ -113,17 +141,25 @@ def function(param1, param2, **kwargs) -> None:
:param kwargs: Pass
"""
''',
'''
"function_with_kwargs.py",
),
(
'''
def function(param1, param2, *args) -> None:
"""Proper docstring."""
''',
'''
"function_with_undocumented_params.py",
),
(
'''
def function() -> None:
"""Proper docstring.

:return: Returncode.
"""
""",
''',
"function_with_documented_return.py",
),
],
ids=[
"c_init",
Expand All @@ -141,11 +177,12 @@ def function() -> None:
],
)
@pytest.mark.benchmark
def test_bench(bench: MockMainType, template: str) -> None:
def test_bench(bench: MockMainType, template: str, path: str) -> None:
"""A small benchmark test.

:param bench: Benchmark fixture that is active when environment
allows it to be.
:param template: String data.
:param path: An associated mock file path.
"""
bench(docsig, string=template)
bench(docsig, path, string=template)
Loading