From e7ff37d1f5e12496a288ef423f4770b5bf352d62 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..57674c18 100644 --- a/twine/settings.py +++ b/twine/settings.py @@ -13,11 +13,13 @@ # 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 Optional from typing import cast +from typing import ContextManager from twine import auth from twine import exceptions @@ -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: