MCPcopy Create free account
hub / github.com/pytest-dev/pytest / fnmatch_ex

Function fnmatch_ex

src/_pytest/pathlib.py:388–421  ·  view source on GitHub ↗

A port of FNMatcher from py.path.common which works with PurePath() instances. The difference between this algorithm and PurePath.match() is that the latter matches "**" glob expressions for each part of the path, while this algorithm uses the whole path instead. For example:

(pattern: str, path: Union[str, "os.PathLike[str]"])

Source from the content-addressed store, hash-verified

386
387
388def fnmatch_ex(pattern: str, path: Union[str, "os.PathLike[str]"]) -> bool:
389 """A port of FNMatcher from py.path.common which works with PurePath() instances.
390
391 The difference between this algorithm and PurePath.match() is that the
392 latter matches "**" glob expressions for each part of the path, while
393 this algorithm uses the whole path instead.
394
395 For example:
396 "tests/foo/bar/doc/test_foo.py" matches pattern "tests/**/doc/test*.py"
397 with this algorithm, but not with PurePath.match().
398
399 This algorithm was ported to keep backward-compatibility with existing
400 settings which assume paths match according this logic.
401
402 References:
403 * https://bugs.python.org/issue29249
404 * https://bugs.python.org/issue34731
405 """
406 path = PurePath(path)
407 iswin32 = sys.platform.startswith("win")
408
409 if iswin32 and sep not in pattern and posix_sep in pattern:
410 # Running on Windows, the pattern has no Windows path separators,
411 # and the pattern has one or more Posix path separators. Replace
412 # the Posix path separators with the Windows path separator.
413 pattern = pattern.replace(posix_sep, sep)
414
415 if sep not in pattern:
416 name = path.name
417 else:
418 name = str(path)
419 if path.is_absolute() and not os.path.isabs(pattern):
420 pattern = f"*{os.sep}{pattern}"
421 return fnmatch.fnmatch(name, pattern)
422
423
424def parts(s: str) -> Set[str]:

Callers 9

_is_doctestFunction · 0.90
path_matches_patternsFunction · 0.90
_recurseMethod · 0.90
_recurseMethod · 0.90
_should_rewriteMethod · 0.90
test_matchingMethod · 0.90
test_matching_abspathMethod · 0.90
test_not_matchingMethod · 0.90

Calls

no outgoing calls

Tested by 4

_is_doctestFunction · 0.72
test_matchingMethod · 0.72
test_matching_abspathMethod · 0.72
test_not_matchingMethod · 0.72