summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAdam J. Stewart <ajstewart426@gmail.com>2022-11-28 06:49:57 -0600
committerGitHub <noreply@github.com>2022-11-28 13:49:57 +0100
commita1b4e1bccd5e4ffb8f0dc7b988453f1eb847ee32 (patch)
treeee22ac2cee900a3950fa2547f3abad35be654db8 /lib
parent066ec316047cb6dfc92a3fef9980718ade47d4aa (diff)
downloadspack-a1b4e1bccd5e4ffb8f0dc7b988453f1eb847ee32.tar.gz
spack-a1b4e1bccd5e4ffb8f0dc7b988453f1eb847ee32.tar.bz2
spack-a1b4e1bccd5e4ffb8f0dc7b988453f1eb847ee32.tar.xz
spack-a1b4e1bccd5e4ffb8f0dc7b988453f1eb847ee32.zip
Add type hints to Prefix class (#34135)
Diffstat (limited to 'lib')
-rw-r--r--lib/spack/spack/util/prefix.py65
1 files changed, 44 insertions, 21 deletions
diff --git a/lib/spack/spack/util/prefix.py b/lib/spack/spack/util/prefix.py
index a4eb30b7c0..a404e626ac 100644
--- a/lib/spack/spack/util/prefix.py
+++ b/lib/spack/spack/util/prefix.py
@@ -7,16 +7,17 @@
This file contains utilities for managing the installation prefix of a package.
"""
import os
+from typing import Dict
class Prefix(str):
- """This class represents an installation prefix, but provides useful
- attributes for referring to directories inside the prefix.
+ """This class represents an installation prefix, but provides useful attributes for referring
+ to directories inside the prefix.
- Attributes of this object are created on the fly when you request them,
- so any of the following is valid:
+ Attributes of this object are created on the fly when you request them, so any of the following
+ are valid:
- >>> prefix = Prefix('/usr')
+ >>> prefix = Prefix("/usr")
>>> prefix.bin
/usr/bin
>>> prefix.lib64
@@ -25,34 +26,56 @@ class Prefix(str):
/usr/share/man
>>> prefix.foo.bar.baz
/usr/foo/bar/baz
- >>> prefix.join('dashed-directory').bin64
+ >>> prefix.join("dashed-directory").bin64
/usr/dashed-directory/bin64
- Prefix objects behave identically to strings. In fact, they
- subclass ``str``. So operators like ``+`` are legal::
+ Prefix objects behave identically to strings. In fact, they subclass ``str``, so operators like
+ ``+`` are legal::
- print('foobar ' + prefix)
+ print("foobar " + prefix)
- This prints ``foobar /usr``. All of this is meant to make custom
- installs easy.
+ This prints ``foobar /usr``. All of this is meant to make custom installs easy.
"""
- def __getattr__(self, attr):
- return Prefix(os.path.join(self, attr))
+ def __getattr__(self, name: str) -> "Prefix":
+ """Concatenate a string to a prefix.
- def join(self, string):
- """Concatenates a string to a prefix.
+ Useful for strings that are valid variable names.
- Parameters:
- string (str): the string to append to the prefix
+ Args:
+ name: the string to append to the prefix
Returns:
- Prefix: the newly created installation prefix
+ the newly created installation prefix
+ """
+ return Prefix(os.path.join(self, name))
+
+ def join(self, string: str) -> "Prefix": # type: ignore[override]
+ """Concatenate a string to a prefix.
+
+ Useful for strings that are not valid variable names. This includes strings containing
+ characters like ``-`` and ``.``.
+
+ Args:
+ string: the string to append to the prefix
+
+ Returns:
+ the newly created installation prefix
"""
return Prefix(os.path.join(self, string))
- def __getstate__(self):
+ def __getstate__(self) -> Dict[str, str]:
+ """Control how object is pickled.
+
+ Returns:
+ current state of the object
+ """
return self.__dict__
- def __setstate__(self, d):
- self.__dict__.update(d)
+ def __setstate__(self, state: Dict[str, str]) -> None:
+ """Control how object is unpickled.
+
+ Args:
+ new state of the object
+ """
+ self.__dict__.update(state)