Coverage for src/pytkdocs/properties.py: 100.00%

16 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-03-09 18:24 +0100

1"""This module simply defines regular expressions and their associated predicates.""" 

2 

3import re 

4from re import Pattern 

5from typing import Callable 

6 

7ApplicableNameProperty = tuple[str, Callable[[str], bool]] 

8 

9# exactly two leading underscores, exactly two trailing underscores 

10# since we enforce one non-underscore after the two leading underscores, 

11# we put the rest in an optional group 

12RE_SPECIAL: Pattern = re.compile(r"^__[^_]([\w_]*[^_])?__$") 

13"""Regular expression to match `__special__` names.""" 

14 

15# at least two leading underscores, at most one trailing underscore 

16# since we enforce one non-underscore before the last, 

17# we make the previous characters optional with an asterisk 

18RE_CLASS_PRIVATE: Pattern = re.compile(r"^__[\w_]*[^_]_?$") 

19"""Regular expression to match `__class_private` names.""" 

20 

21# at most one leading underscore, then whatever 

22RE_PRIVATE: Pattern = re.compile(r"^_[^_][\w_]*$") 

23"""Regular expression to match `_private` names.""" 

24 

25NAME_SPECIAL: ApplicableNameProperty = ("special", lambda name: bool(RE_SPECIAL.match(name))) 

26"""Applicable property: `special`.""" 

27 

28NAME_CLASS_PRIVATE: ApplicableNameProperty = ("class-private", lambda name: bool(RE_CLASS_PRIVATE.match(name))) 

29"""Applicable property: `class-private`.""" 

30 

31NAME_PRIVATE: ApplicableNameProperty = ("private", lambda name: bool(RE_PRIVATE.match(name))) 

32"""Applicable property: `private`."""