mercurial/typelib.py
author Matt Harbison <matt_harbison@yahoo.com>
Thu, 18 Jul 2024 19:57:42 -0400
changeset 51716 f3b34386d3e0
parent 49793 8147abc05794
child 51732 43460c311c0c
permissions -rw-r--r--
typing: add type hints to `mercurial.dirstatemap` Somewhere since 10db46e128d4, pytype stopped being able to infer the type of the `identity` field. Fill in some obvious other hints along the way. These hints caused pytype to flag a missing attribute: File "/mnt/c/Users/Matt/hg/mercurial/dirstatemap.py", line 714, in _v1_map: No attribute 'stat' on mercurial.windows.cachestat [attribute-error] In Union[Any, mercurial.posix.cachestat, mercurial.windows.cachestat] File "/mnt/c/Users/Matt/hg/mercurial/dirstatemap.py", line 715, in _v1_map: No attribute 'stat' on mercurial.windows.cachestat [attribute-error] In Union[Any, mercurial.posix.cachestat, mercurial.windows.cachestat] In practice, the `identity` field is NOT replaced with None if it isn't cacheable, so it's probably safer to just add the field and set it to None, since that check is already in place on line 715.

# typelib.py - type hint aliases and support
#
# Copyright 2022 Matt Harbison <matt_harbison@yahoo.com>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.

import typing

# Note: this is slightly different from pycompat.TYPE_CHECKING, as using
# pycompat causes the BinaryIO_Proxy type to be resolved to ``object`` when
# used as the base class during a pytype run.
TYPE_CHECKING = typing.TYPE_CHECKING


# The BinaryIO class provides empty methods, which at runtime means that
# ``__getattr__`` on the proxy classes won't get called for the methods that
# should delegate to the internal object.  So to avoid runtime changes because
# of the required typing inheritance, just use BinaryIO when typechecking, and
# ``object`` otherwise.
if TYPE_CHECKING:
    from typing import (
        BinaryIO,
        Union,
    )

    from . import (
        node,
        posix,
        windows,
    )

    BinaryIO_Proxy = BinaryIO
    CacheStat = Union[posix.cachestat, windows.cachestat]
    NodeConstants = node.sha1nodeconstants
else:
    from typing import Any

    BinaryIO_Proxy = object
    CacheStat = Any
    NodeConstants = Any