communex.password
1from typing import Protocol 2 3from communex.errors import PasswordNotProvidedError 4 5 6class PasswordProvider(Protocol): 7 def get_password(self, key_name: str) -> str | None: 8 """ 9 Provides a password for the given key name, if it is know. If not, 10 returns None. In that case, `ask_password` can be called to ask for the 11 password depending on the implementation. 12 """ 13 return None 14 15 def ask_password(self, key_name: str) -> str: 16 """ 17 Either provides a password for the given key or raises an 18 PasswordNotProvidedError error. 19 """ 20 raise PasswordNotProvidedError( 21 f"Password not provided for key '{key_name}'" 22 ) 23 24 25class NoPassword(PasswordProvider): 26 pass 27 28 29class Password(PasswordProvider): 30 def __init__(self, password: str): 31 self._password = password 32 33 def get_password(self, key_name: str) -> str: 34 return self._password 35 36 def ask_password(self, key_name: str) -> str: 37 return self._password
7class PasswordProvider(Protocol): 8 def get_password(self, key_name: str) -> str | None: 9 """ 10 Provides a password for the given key name, if it is know. If not, 11 returns None. In that case, `ask_password` can be called to ask for the 12 password depending on the implementation. 13 """ 14 return None 15 16 def ask_password(self, key_name: str) -> str: 17 """ 18 Either provides a password for the given key or raises an 19 PasswordNotProvidedError error. 20 """ 21 raise PasswordNotProvidedError( 22 f"Password not provided for key '{key_name}'" 23 )
Base class for protocol classes.
Protocol classes are defined as::
class Proto(Protocol):
def meth(self) -> int:
...
Such classes are primarily used with static type checkers that recognize structural subtyping (static duck-typing).
For example::
class C:
def meth(self) -> int:
return 0
def func(x: Proto) -> int:
return x.meth()
func(C()) # Passes static type check
See PEP 544 for details. Protocol classes decorated with @typing.runtime_checkable act as simple-minded runtime protocols that check only the presence of given attributes, ignoring their type signatures. Protocol classes can be generic, they are defined as::
class GenProto(Protocol[T]):
def meth(self) -> T:
...
1927def _no_init_or_replace_init(self, *args, **kwargs): 1928 cls = type(self) 1929 1930 if cls._is_protocol: 1931 raise TypeError('Protocols cannot be instantiated') 1932 1933 # Already using a custom `__init__`. No need to calculate correct 1934 # `__init__` to call. This can lead to RecursionError. See bpo-45121. 1935 if cls.__init__ is not _no_init_or_replace_init: 1936 return 1937 1938 # Initially, `__init__` of a protocol subclass is set to `_no_init_or_replace_init`. 1939 # The first instantiation of the subclass will call `_no_init_or_replace_init` which 1940 # searches for a proper new `__init__` in the MRO. The new `__init__` 1941 # replaces the subclass' old `__init__` (ie `_no_init_or_replace_init`). Subsequent 1942 # instantiation of the protocol subclass will thus use the new 1943 # `__init__` and no longer call `_no_init_or_replace_init`. 1944 for base in cls.__mro__: 1945 init = base.__dict__.get('__init__', _no_init_or_replace_init) 1946 if init is not _no_init_or_replace_init: 1947 cls.__init__ = init 1948 break 1949 else: 1950 # should not happen 1951 cls.__init__ = object.__init__ 1952 1953 cls.__init__(self, *args, **kwargs)
8 def get_password(self, key_name: str) -> str | None: 9 """ 10 Provides a password for the given key name, if it is know. If not, 11 returns None. In that case, `ask_password` can be called to ask for the 12 password depending on the implementation. 13 """ 14 return None
Provides a password for the given key name, if it is know. If not,
returns None. In that case, ask_password
can be called to ask for the
password depending on the implementation.
16 def ask_password(self, key_name: str) -> str: 17 """ 18 Either provides a password for the given key or raises an 19 PasswordNotProvidedError error. 20 """ 21 raise PasswordNotProvidedError( 22 f"Password not provided for key '{key_name}'" 23 )
Either provides a password for the given key or raises an PasswordNotProvidedError error.
Base class for protocol classes.
Protocol classes are defined as::
class Proto(Protocol):
def meth(self) -> int:
...
Such classes are primarily used with static type checkers that recognize structural subtyping (static duck-typing).
For example::
class C:
def meth(self) -> int:
return 0
def func(x: Proto) -> int:
return x.meth()
func(C()) # Passes static type check
See PEP 544 for details. Protocol classes decorated with @typing.runtime_checkable act as simple-minded runtime protocols that check only the presence of given attributes, ignoring their type signatures. Protocol classes can be generic, they are defined as::
class GenProto(Protocol[T]):
def meth(self) -> T:
...
Inherited Members
30class Password(PasswordProvider): 31 def __init__(self, password: str): 32 self._password = password 33 34 def get_password(self, key_name: str) -> str: 35 return self._password 36 37 def ask_password(self, key_name: str) -> str: 38 return self._password
Base class for protocol classes.
Protocol classes are defined as::
class Proto(Protocol):
def meth(self) -> int:
...
Such classes are primarily used with static type checkers that recognize structural subtyping (static duck-typing).
For example::
class C:
def meth(self) -> int:
return 0
def func(x: Proto) -> int:
return x.meth()
func(C()) # Passes static type check
See PEP 544 for details. Protocol classes decorated with @typing.runtime_checkable act as simple-minded runtime protocols that check only the presence of given attributes, ignoring their type signatures. Protocol classes can be generic, they are defined as::
class GenProto(Protocol[T]):
def meth(self) -> T:
...
Provides a password for the given key name, if it is know. If not,
returns None. In that case, ask_password
can be called to ask for the
password depending on the implementation.