Skip to content

Commit

Permalink
TST: Test no-file for source (#493)
Browse files Browse the repository at this point in the history
* TST: Test no-file for source

* ENH: Speed up and simplify

* FIX: Events
  • Loading branch information
larsoner authored Aug 21, 2023
1 parent 41a1508 commit d9a3dfe
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/label-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ on:
pull_request:
types:
- opened
- repoened
- labeled
- unlabeled
- synchronize

env:
LABELS: ${{ join( github.event.pull_request.labels.*.name, ' ' ) }}
Expand Down
10 changes: 10 additions & 0 deletions numpydoc/tests/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ def test_get_validation_checks_validity(checks):
_ = validate.get_validation_checks(checks)


class _DummyList(list):
"""Dummy list class to test validation."""


def test_no_file():
"""Test that validation can be done on functions made on the fly."""
# Just a smoke test for now, <list> will have a None filename
validate.validate("numpydoc.tests.test_validate._DummyList.clear")


@pytest.mark.parametrize(
["file_contents", "expected"],
[
Expand Down
21 changes: 17 additions & 4 deletions numpydoc/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
"""

from copy import deepcopy
from typing import Dict, List, Set
from typing import Dict, List, Set, Optional
import ast
import collections
import functools
import importlib
import inspect
import os
Expand Down Expand Up @@ -111,7 +112,15 @@
IGNORE_COMMENT_PATTERN = re.compile("(?:.* numpydoc ignore[=|:] ?)(.+)")


def extract_ignore_validation_comments(filepath: os.PathLike) -> Dict[int, List[str]]:
# This function gets called once per function/method to be validated.
# We have to balance memory usage with performance here. It shouldn't be too
# bad to store these `dict`s (they should be rare), but to be safe let's keep
# the limit low-ish. This was set by looking at scipy, numpy, matplotlib,
# and pandas and they had between ~500 and ~1300 .py files as of 2023-08-16.
@functools.lru_cache(maxsize=2000)
def extract_ignore_validation_comments(
filepath: Optional[os.PathLike],
) -> Dict[int, List[str]]:
"""
Extract inline comments indicating certain validation checks should be ignored.
Expand All @@ -125,8 +134,12 @@ def extract_ignore_validation_comments(filepath: os.PathLike) -> Dict[int, List[
dict[int, list[str]]
Mapping of line number to a list of checks to ignore.
"""
with open(filepath) as file:
numpydoc_ignore_comments = {}
numpydoc_ignore_comments = {}
try:
file = open(filepath)
except (OSError, TypeError): # can be None, nonexistent, or unreadable
return numpydoc_ignore_comments
with file:
last_declaration = 1
declarations = ["def", "class"]
for token in tokenize.generate_tokens(file.readline):
Expand Down

0 comments on commit d9a3dfe

Please sign in to comment.