From e15122e027bc931c5a5fa6c5307b98e3f6c434ae Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 9 Aug 2020 21:03:11 -0400 Subject: [PATCH] Extract _allow_noninteractive method to suppress NonInteractive error. --- twine/settings.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/twine/settings.py b/twine/settings.py index d7128687..2dac15ef 100644 --- a/twine/settings.py +++ b/twine/settings.py @@ -13,9 +13,11 @@ # See the License for the specific language governing permissions and # limitations under the License. import argparse +import contextlib import logging import sys from typing import Any +from typing import ContextManager from typing import Optional from typing import cast @@ -144,14 +146,14 @@ def username(self) -> Optional[str]: @property def password(self) -> Optional[str]: - if self.client_cert: - try: - return cast(Optional[str], self.auth.password) - except exceptions.NonInteractive: - return None - - # Workaround for https://github.com/python/mypy/issues/5858 - return cast(Optional[str], self.auth.password) + with self._allow_noninteractive(): + # Workaround for https://github.com/python/mypy/issues/5858 + return cast(Optional[str], self.auth.password) + + def _allow_noninteractive(self) -> ContextManager[None]: + """Bypass NonInteractive error when client cert is present.""" + suppressed = (exceptions.NonInteractive,) if self.client_cert else () + return contextlib.suppress(*suppressed) @property def verbose(self) -> bool: