From acd20ca332bb3f0ad48c7d92f832bfb2121decc1 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Fri, 20 Mar 2015 15:34:18 -0400 Subject: Reindent warning and use short circuits for flatter implementation. --- pkg_resources/__init__.py | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/pkg_resources/__init__.py b/pkg_resources/__init__.py index 1bb67a71..f7611472 100644 --- a/pkg_resources/__init__.py +++ b/pkg_resources/__init__.py @@ -36,6 +36,7 @@ import collections import plistlib import email.parser import tempfile +import textwrap from pkgutil import get_importer PY3 = sys.version_info > (3,) @@ -2505,26 +2506,29 @@ class Distribution(object): return self._parsed_version def _warn_legacy_version(self): - if isinstance( - self._parsed_version, packaging.version.LegacyVersion): - # While an empty version is techincally a legacy version and - # is not a valid PEP 440 version, it's also unlikely to - # actually come from someone and instead it is more likely that - # it comes from setuptools attempting to parse a filename and - # including it in the list. So for that we'll gate this warning - # on if the version is anything at all or not. - if self.version: - warnings.warn( - "'%s (%s)' is being parsed as a legacy, non PEP 440, " - "version. You may find odd behavior and sort order. " - "In particular it will be sorted as less than 0.0. It " - "is recommend to migrate to PEP 440 compatible " - "versions." % ( - self.project_name, self.version, - ), - PEP440Warning, - ) + LV = packaging.version.LegacyVersion + is_legacy = isinstance(self._parsed_version, LV) + if not is_legacy: + return + + # While an empty version is techincally a legacy version and + # is not a valid PEP 440 version, it's also unlikely to + # actually come from someone and instead it is more likely that + # it comes from setuptools attempting to parse a filename and + # including it in the list. So for that we'll gate this warning + # on if the version is anything at all or not. + if not self.version: + return + + tmpl = textwrap.dedent(""" + '%s (%s)' is being parsed as a legacy, non PEP 440, + version. You may find odd behavior and sort order. + In particular it will be sorted as less than 0.0. It + is recommend to migrate to PEP 440 compatible + versions. + """).strip().replace('\n', ' ') + warnings.warn(tmpl % (self.project_name, self.version), PEP440Warning) @property def version(self): -- cgit v1.2.3