Skip to content

Latest commit

 

History

History
32 lines (27 loc) · 1.37 KB

File metadata and controls

32 lines (27 loc) · 1.37 KB

I learn about python and ground those lessons in the pytest codebase here.

Entry Point Object Lessons
searched
(itertools.)?chain\(
resolve_package_path - itertools.chain takes a variable number of iterables.
- was likely chosen because it does not create a list in memory
- Path has methods that do system calls.
- reaching the end of multiple if not conditions is the same as 'If all conditions are true'
- path.parents is a sequence of Paths
                                              |
resolve_package_path
#src/_pytest/pathlib.py
def resolve_package_path(path: Path) -> Optional[Path]:
    """Return the Python package path by looking for the last
    directory upwards which still contains an __init__.py.

    Returns None if it can not be determined.
    """
    result = None
    for parent in itertools.chain((path,), path.parents):
        if parent.is_dir():
            if not parent.joinpath("__init__.py").is_file():
                break
            if not parent.name.isidentifier():
                break
            result = parent
    return result

Observe that itertools.chain requires a variable number of iterable arguments.

Warning

Unpacking is eager.