From 0ae9b232b82285f2fa275b8ffa5dced6b9377b0e Mon Sep 17 00:00:00 2001 From: wwuck Date: Thu, 25 Nov 2021 16:01:39 +1100 Subject: [PATCH 1/4] Add equality operator to EnvironCredential Equality operator is useful for testing EnvironCredential --- keyring/credentials.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/keyring/credentials.py b/keyring/credentials.py index 8c96fa9a..9b783f0f 100644 --- a/keyring/credentials.py +++ b/keyring/credentials.py @@ -39,6 +39,15 @@ def __init__(self, user_env_var, pwd_env_var): self.user_env_var = user_env_var self.pwd_env_var = pwd_env_var + def __eq__(self, other: object) -> bool: + if not isinstance(other, EnvironCredential): + return NotImplemented + + return ( + self.user_env_var == other.user_env_var + and self.pwd_env_var == other.pwd_env_var + ) + def _get_env(self, env_var): """Helper to read an environment variable""" value = os.environ.get(env_var) From 486b9a8da04045897796161861d062cd753f81ee Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 28 Nov 2021 13:52:32 -0500 Subject: [PATCH 2/4] Simply compare vars for equality in EnvironCredential. --- keyring/credentials.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/keyring/credentials.py b/keyring/credentials.py index 9b783f0f..2ca55612 100644 --- a/keyring/credentials.py +++ b/keyring/credentials.py @@ -40,13 +40,7 @@ def __init__(self, user_env_var, pwd_env_var): self.pwd_env_var = pwd_env_var def __eq__(self, other: object) -> bool: - if not isinstance(other, EnvironCredential): - return NotImplemented - - return ( - self.user_env_var == other.user_env_var - and self.pwd_env_var == other.pwd_env_var - ) + return vars(self) == vars(other) def _get_env(self, env_var): """Helper to read an environment variable""" From c43d489eee32ee322a508aca45e99ca33ff44bc5 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 28 Nov 2021 13:55:14 -0500 Subject: [PATCH 3/4] Add tests for comparison for equality. --- keyring/credentials.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/keyring/credentials.py b/keyring/credentials.py index 2ca55612..933b9d4d 100644 --- a/keyring/credentials.py +++ b/keyring/credentials.py @@ -31,8 +31,20 @@ def password(self): class EnvironCredential(Credential): - """Source credentials from environment variables. + """ + Source credentials from environment variables. + Actual sourcing is deferred until requested. + + Supports comparison by equality. + + >>> e1 = EnvironCredential('a', 'b') + >>> e2 = EnvironCredential('a', 'b') + >>> e3 = EnvironCredential('a', 'c') + >>> e1 == e2 + True + >>> e2 == e3 + False """ def __init__(self, user_env_var, pwd_env_var): From b3f9f2caab19ba1f57b8a4486b8b9ca18cf1e1db Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 28 Nov 2021 13:55:50 -0500 Subject: [PATCH 4/4] Update changelog. --- CHANGES.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 249cc054..e5f092e3 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,3 +1,9 @@ +v23.4.0 +------- + +* #549: EnvironCredential now allows for equality + comparison. + v23.3.0 -------