Skip to content

Commit c31b9b5

Browse files
authored
Allow pathlib.Path object to be used in ignore items (#48)
1 parent a149ab3 commit c31b9b5

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

docs/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## v0.6.3
22
- Fix standard library ignoring ignores 3rd-party libraries under site-packages/
3+
- Allow pathlib.Path object to be used in ignore items
34

45
## v0.6.2
56
- Remove argument `full` for `nameof`, use `vars_only` instead. When `vars_only=False`, source of the argument returned.

tests/test_varname.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,13 +374,13 @@ def test_ignore_dirname(tmp_path):
374374
'ignore_dirname',
375375
"""
376376
from varname import varname
377-
def bar():
378-
return varname(ignore=[%r])
379-
""" % tmp_path.as_posix(),
377+
def bar(dirname):
378+
return varname(ignore=[dirname])
379+
""",
380380
tmp_path
381381
)
382382
def foo():
383-
return module.bar()
383+
return module.bar(tmp_path)
384384

385385
f = foo()
386386
assert f == 'f'

varname/ignore.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import distutils.sysconfig as sysconfig
2323
import warnings
2424
from os import path
25+
from pathlib import Path
2526
from fnmatch import fnmatch
2627
from abc import ABC, abstractmethod
2728
from typing import List, Optional, Union
@@ -116,6 +117,8 @@ class IgnoreDirname(IgnoreElem, attrs=['dirname']):
116117
def _post_init(self) -> None:
117118
# pylint: disable=access-member-before-definition
118119
# pylint: disable=attribute-defined-outside-init
120+
121+
# Path object will turn into str here
119122
self.dirname = path.realpath(self.dirname)
120123

121124
if not self.dirname.endswith(path.sep):
@@ -247,7 +250,7 @@ def create_ignore_elem(ignore_elem: IgnoreElemType) -> IgnoreElem:
247250
"""Create an ignore element according to the type"""
248251
if isinstance(ignore_elem, ModuleType):
249252
return IgnoreModule(ignore_elem)
250-
if isinstance(ignore_elem, str):
253+
if isinstance(ignore_elem, (Path, str)):
251254
return (IgnoreDirname(ignore_elem)
252255
if path.isdir(ignore_elem)
253256
else IgnoreFilename(ignore_elem))
@@ -267,7 +270,7 @@ def create_ignore_elem(ignore_elem: IgnoreElemType) -> IgnoreElem:
267270

268271
if isinstance(ignore_elem[0], ModuleType):
269272
return IgnoreModuleQualname(*ignore_elem)
270-
if isinstance(ignore_elem[0], str):
273+
if isinstance(ignore_elem[0], (Path, str)):
271274
return IgnoreFilenameQualname(*ignore_elem)
272275
if ignore_elem[0] is None:
273276
return IgnoreOnlyQualname(*ignore_elem)

varname/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import warnings
1616
import inspect
1717
from os import path
18+
from pathlib import Path
1819
from functools import lru_cache
1920
from types import ModuleType, FunctionType, CodeType, FrameType
2021
from typing import Optional, Tuple, Union, List, MutableMapping, Callable
@@ -26,6 +27,7 @@
2627
ModuleType,
2728
# filename of a module
2829
str,
30+
Path,
2931
FunctionType,
3032
# the module (filename) and qualname
3133
# If module is None, then all qualname matches the 2nd element

0 commit comments

Comments
 (0)