Skip to content

Commit

Permalink
Removed dependency on 'six' package; cleaned up conditions for DictVi…
Browse files Browse the repository at this point in the history
…ew methods

Signed-off-by: Andreas Maier <andreas.r.maier@gmx.de>
  • Loading branch information
andy-maier committed Apr 12, 2021
1 parent 06b69bc commit 4f4a4aa
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 22 deletions.
5 changes: 5 additions & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ readme-renderer>=23.0; python_version >= '3.5'

# Indirect dependencies with special constraints:

# six (also used by virtualenv, tox, probably others)
# virtualenv 20.0 requires six>=1.12.0 on py>=3.8
# tox 3.17 requires six>=1.14.0
six>=1.14.0

# pytz (used by TBD)
pytz>=2016.10

Expand Down
2 changes: 2 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Released: not yet

**Enhancements:**

* Removed dependency to 'six' package.

**Cleanup:**

* Docs: Simplified the introduction section.
Expand Down
28 changes: 18 additions & 10 deletions immutable_views/_dict_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,26 @@

from __future__ import print_function, absolute_import

import sys
import os
try:
from collections.abc import Mapping
except ImportError:
# Python 2
from collections import Mapping
import six

__all__ = ['DictView']

# This env var is set when building the docs. It causes the methods
# that are supposed to exist only in a particular Python version, not to be
# removed, so they appear in the docs.
BUILDING_DOCS = os.environ.get('BUILDING_DOCS', False)
# This env var is set when building the docs. It causes the methods that are
# supposed to exist only under certain circumstances, not to be removed, so
# that they appear in the docs.
_BUILDING_DOCS = os.environ.get('BUILDING_DOCS', False)

# Indicates Python dict supports the iter..() and view..() methods
_DICT_SUPPORTS_ITER_VIEW = sys.version_info[0:2] == (2, 7)

# Indicates Python dict supports the has_key() method
_DICT_SUPPORTS_HAS_KEY = sys.version_info[0:2] == (2, 7)


class DictView(Mapping):
Expand Down Expand Up @@ -415,14 +421,16 @@ def __le__(self, other):
return self._dict <= other_dict


# Remove methods that should only be present in a particular Python version
# If the documentation is built, the methods are not removed in order to show
# them in the documentation.
if not six.PY2 and not BUILDING_DOCS:
del DictView.has_key
# Remove methods that should only be present under certain circumstances,
# except if the documentation is built.

if not _DICT_SUPPORTS_ITER_VIEW and not _BUILDING_DOCS:
del DictView.iterkeys
del DictView.itervalues
del DictView.iteritems
del DictView.viewkeys
del DictView.viewvalues
del DictView.viewitems

if not _DICT_SUPPORTS_HAS_KEY and not _BUILDING_DOCS:
del DictView.has_key
5 changes: 1 addition & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@

# Direct dependencies (except pip, setuptools, wheel):

# six (also used by virtualenv, tox, probably others)
# virtualenv 20.0 requires six>=1.12.0 on py>=3.8
# tox 3.17 requires six>=1.14.0
six>=1.14.0
# None


# Indirect dependencies are not specified in this file.
19 changes: 11 additions & 8 deletions tests/unittest/test_dict_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
# Indicates Python dict supports the iter..() and view..() methods
DICT_SUPPORTS_ITER_VIEW = sys.version_info[0:2] == (2, 7)

# Indicates Python dict supports the has_key() method
DICT_SUPPORTS_HAS_KEY = sys.version_info[0:2] == (2, 7)

# Used as indicator not to pass an argument in the testcases.
# Note this has nothing to do with the _OMITTED flag in _immutable_views.py and
# could be a different value.
Expand Down Expand Up @@ -643,7 +646,7 @@ def test_DictView_contains(testcase, obj, key, exp_result):
key=None,
exp_result=False,
),
AttributeError if not PY2 else None, None, True
AttributeError if not DICT_SUPPORTS_HAS_KEY else None, None, True
),
(
"Empty dict, with integer key (no lower / success)",
Expand All @@ -652,7 +655,7 @@ def test_DictView_contains(testcase, obj, key, exp_result):
key=1234,
exp_result=False,
),
AttributeError if not PY2 else None, None, True
AttributeError if not DICT_SUPPORTS_HAS_KEY else None, None, True
),
(
"Empty dict, with empty string key (not found)",
Expand All @@ -661,7 +664,7 @@ def test_DictView_contains(testcase, obj, key, exp_result):
key='',
exp_result=False,
),
AttributeError if not PY2 else None, None, True
AttributeError if not DICT_SUPPORTS_HAS_KEY else None, None, True
),
(
"Empty dict, with non-empty key (not found)",
Expand All @@ -670,7 +673,7 @@ def test_DictView_contains(testcase, obj, key, exp_result):
key='Dog',
exp_result=False,
),
AttributeError if not PY2 else None, None, True
AttributeError if not DICT_SUPPORTS_HAS_KEY else None, None, True
),

# Non-empty DictView
Expand All @@ -681,7 +684,7 @@ def test_DictView_contains(testcase, obj, key, exp_result):
key=None,
exp_result=False,
),
AttributeError if not PY2 else None, None, True
AttributeError if not DICT_SUPPORTS_HAS_KEY else None, None, True
),
(
"Non-empty dict, with empty string key (not found)",
Expand All @@ -690,7 +693,7 @@ def test_DictView_contains(testcase, obj, key, exp_result):
key='',
exp_result=False,
),
AttributeError if not PY2 else None, None, True
AttributeError if not DICT_SUPPORTS_HAS_KEY else None, None, True
),
(
"Non-empty dict, with non-empty key (not found)",
Expand All @@ -699,7 +702,7 @@ def test_DictView_contains(testcase, obj, key, exp_result):
key='invalid',
exp_result=False,
),
AttributeError if not PY2 else None, None, True
AttributeError if not DICT_SUPPORTS_HAS_KEY else None, None, True
),
(
"Non-empty dict, with existing key",
Expand All @@ -708,7 +711,7 @@ def test_DictView_contains(testcase, obj, key, exp_result):
key='Dog',
exp_result=True,
),
AttributeError if not PY2 else None, None, True
AttributeError if not DICT_SUPPORTS_HAS_KEY else None, None, True
),
]

Expand Down

0 comments on commit 4f4a4aa

Please sign in to comment.