1import operator
2import os
3import pathlib
4import sys
5import types
6import typing
7
8
9def wrap(path): # pragma: no cover
10 """
11 Workaround for https://github.com/python/cpython/issues/84538
12 to add backward compatibility for walk_up=True.
13 An example affected package is dask-labextension, which uses
14 jupyter-packaging to install JupyterLab javascript files outside
15 of site-packages.
16 """
17
18 def relative_to(root, *, walk_up=False):
19 return pathlib.Path(os.path.relpath(path, root))
20
21 return types.SimpleNamespace(relative_to=relative_to)
22
23
24relative_fix = wrap if sys.version_info < (3, 12) else lambda x: x
25
26
27# __protocol_attrs__ is only present on Python 3.12 and later.
28protocol_attrs = (
29 typing._get_protocol_attrs # type: ignore[attr-defined] # private, 3.11 only
30 if sys.version_info < (3, 12)
31 else operator.attrgetter('__protocol_attrs__')
32)