Commit Graph

101 Commits

Author SHA1 Message Date
leonidgman 7e4c8f2f66 Add pbt's for duplication errors (#116) 2020-09-27 19:37:37 -04:00
Joshua Bronson c1f9403d47 Add pbt: test_cleared_bidicts_have_no_items (#116) 2020-09-27 13:39:47 -04:00
Joshua Bronson 77b9faf9e9 Small docs + test improvements + prepare for new release. 2020-08-22 10:32:51 -04:00
Joshua Bronson 0ad5284f75 Finish first pass at adding type hints.
Fixes #93.
2020-08-12 10:22:25 -04:00
Joshua Bronson 8a8e58b51f f-strings 2020-08-01 09:54:45 -04:00
Joshua Bronson 01460c86a3 reduce noisy pylint comments 2020-08-01 09:44:24 -04:00
Joshua Bronson e62d85d933 Begin adding type hints, drop Python 3.5, rm bidict.compat 2020-08-01 09:31:15 -04:00
Joshua Bronson 2efdc0e7b8 Upgrade dependencies for various extras, precommit hooks, CI
Use a single job that runs all precommit hooks in CI
rather than separate jobs for flake8, pylint, pydocstyle, etc.
2020-07-29 21:59:45 -04:00
Joshua Bronson e2514160ec Add MutableBidirectionalMapping ABC 2020-07-26 16:33:25 -04:00
Joshua Bronson 5a4b6e99f4 Remove BidirectionalMapping.__subclasshook__
...due to lack of use and maintenance cost.

Fixes #111.
2020-07-23 09:37:53 -04:00
jab 9bb1d88383 rm PY2 vestige for namedbidict tests missed by #99. 2020-01-11 12:29:57 -05:00
jab fe269a351d Fix mis-ported @examples from 0ec3b8b.
Should make coverage more consistent again.
2020-01-10 21:49:04 -05:00
jab 9ade536188 rm IGNORE/OVERWRITE and on_dup_* compat code. 2020-01-08 21:48:51 -05:00
jab 0ec3b8b3e7 Various API changes and other improvements.
* Deprecate ``bidict.OVERWRITE`` and ``bidict.IGNORE``.
  A :class:`UserWarning` will now be emitted if these are used.

  :attr:`bidict.DROP_OLD` and :attr:`bidict.DROP_NEW` should be used instead.

* Rename ``DuplicationPolicy`` to :class:`~bidict.OnDupAction`
  (and implement it via an :class:`~enum.Enum`).

  A :class:`~bidict.OnDupAction` may be one of
  :attr:`~bidict.RAISE`,
  :attr:`~bidict.DROP_OLD`, or
  :attr:`~bidict.DROP_NEW`.

* Expose the new :class:`~bidict.OnDup` class,
  a named (*key*, *val*, *kv*) tuple of :class:`~bidict.OnDupAction`\s
  that should be taken upon encountering
  the 3 kinds of duplication that can occur.

* Provide the
  :attr:`~bidict.ON_DUP_DEFAULT`,
  :attr:`~bidict.ON_DUP_RAISE`, and
  :attr:`~bidict.ON_DUP_DROP_OLD`
  :class:`~bidict.OnDup` convenience instances.

* Deprecate the
  ``on_dup_key``, ``on_dup_val``, and ``on_dup_kv`` arguments
  of :meth:`~bidict.bidict.put` and :meth:`~bidict.bidict.putall`.
  A :class:`UserWarning` will now be emitted if these are used.

  They have been subsumed by the new *on_dup* argument,
  which takes an :class:`~bidict.OnDup` instance.
  Use it like this: ``bi.put(1, 2, OnDup(key=DROP_NEW))``.
  Or better yet, pass one of the
  ``ON_DUP_*`` convenience instances
  instead if possible.

  See the updated :ref:`basic-usage:Values Must Be Unique` docs for more info.

* Deprecate the
  ``on_dup_key``, ``on_dup_val``, and ``on_dup_kv``
  bidict class attributes.
  A :class:`UserWarning` will now be emitted if these are used.

  They have been subsumed by the new
  :attr:`~bidict.bidict.on_dup` class attribute,
  which takes an :class:`~bidict.OnDup` instance.

  See the updated :doc:`extending` docs for example usage.

* Move
  :meth:`bidict.BidictBase.values` to
  :meth:`bidict.BidirectionalMapping.values`,
  since the implementation is generic.

* No longer use ``__all__`` in ``bidict/__init__.py``.

* Cap max_size rather than disabling health checks and deadline as a
  less heavyhanded way to improve hypothesis test reliability on Travis.
2020-01-07 17:20:25 -05:00
jab d2b5adcce3 Happy 2020 🎉 2019-12-31 18:14:38 -05:00
jab a836f64aa4 Remove more Python 2 vestiges missed by #99. 2019-11-09 19:36:54 -05:00
jab cd73edd854 Remove Python 2 support.
Closes #97.
2019-11-03 21:54:54 -05:00
jab d925bd1c52 Add deterministic test cases for _undo_write.
Add deterministic inputs to test_putall_same_as_put_for_each_item via
@hypothesis.example(...) to ensure deterministic coverage of branches
in BidictBase._undo_write. Hypothesis doesn't always generate examples
that hit all the branches otherwise.
2019-11-03 21:05:46 -05:00
jab b207e1467a Improve Hypothesis tests.
- Initialize from iterables instead of lists of items in more places.
- Generate arbitrarily many items (rather than max 10).
- Don't include floats and text in HASHABLES. Slows example generation
  without actually finding more falsifying examples.
- Use simpler and more concise `st.tuples(...).map(...).filter(...)`
  constructions rather than much more complicated `st.composite`-based
  strategies.
- Simplify `test_iteritems_args_kw`.
- Split out more namedbidict tests so each one tests one thing.
- Use latest Travis pypy and pypy3.
- Only use a single Hypothesis profile ("custom"). Allow any of its
  settings that should be tunable to be tuned via dedicated env vars
  (currently just "HYPOTHESIS_MAX_EXAMPLES").
- Always turn off "too_slow" health check, not just for Travis cron builds.
  Never want a build to fail only because tests took too long.
- Set --hypothesis-verbosity="verbose" for Travis cron builds.
- Set --hypothesis-show-statistics for Travis cron builds.
2019-09-15 14:19:00 -04:00
jab ef7d75b488 Improve validation of names passed to namedbidict.
Use str.isidentifier on Python 3,
and a better regex on Python 2.

Revision 033083 (released in 0.15.0)
incorrectly replaced "A-Za-z" with "A-z" in the regex.
Also, "_" should always have been allowed
for the first character, but previously wasn't.
2019-09-08 17:43:57 -04:00
jab 38bef0eaa2 copy.copy and copy.deepcopy doctests 2019-09-03 10:26:35 -04:00
jab 94d42de53b Fix #94 - buggy unpickling and deepcopying.
Also fix some docs links broken in the previous commit.
2019-09-02 17:07:03 -04:00
jab 7a1069c35c Small improvements to tests, CI, docs + upgrades 2019-09-02 15:51:36 -04:00
jab 8600253254 Fix `UnboundLocalError: local variable 'ref' referenced before assignment` on Travis 2019-06-02 20:47:13 -04:00
jab 1e34ae0a24 Use ItemsView in _bidict_and_mapping_from_items when not same_items for portability.
e.g. on PyPy:

    >>>> b = bidict({1: 2, 3: 4})
    >>>> b.items()
    [(1, 2), (3, 4)]
    >>>> m = {3: 4, 1: 2}
    >>>> m.items()
    [(3, 4), (1, 2)]
    >>>> b.items() == m.items()
    False
    >>>> ItemsView(b) == ItemsView(m)
    True
2019-04-14 17:16:15 -04:00
jab ed475d5727 fix example generation for hypothesis test namedbidict_raises_on_invalid_name 2019-03-22 13:46:19 -04:00
jab 1eb330ae34 Fix `assume` for BIDICT_AND_MAPPING_FROM_DIFFERENT_ITEMS hypothesis strategy.
Should not be order-sensitive.

Must now suppress `too_slow` health check for `test_unequal_to_mapping_with_different_items()`
2019-03-03 14:36:54 -05:00
jab ce345d49ba Rename .inv to .inverse and add an alias for .inv.
bidict.BidirectionalMapping.__subclasshook__ now requires an ``inverse`` attribute
rather than an ``inv`` attribute for a class to qualify as a virtual subclass.

Closes #86.
2019-02-13 19:50:22 -05:00
jab ba1cba8523 happy 2019 2019-01-01 13:51:46 -05:00
jab a3fd16b6f7 Travis build improvements. 2018-12-30 15:03:42 -05:00
jab 36e28358dc improve hypothesis tests and .travis.yml, add compat.collections_abc and namedbidict.{_keyname,_valname} 2018-12-29 13:13:06 -05:00
jab d13fd9bb8a resurrect delegating mixins instead of __delegate__ + other refinements
e.g. s/__repr_delegate__/_repr_delegate

Also
- update
- update from pytest 3 to 4
- add CII best practices badge
- prepare for 0.17.5 release
2018-11-19 09:37:38 -05:00
jab ba6e1fff13 Add tests for better bidict.compat.{iter,view}* compatibility
No longer assume the target object implements these methods on Python 2.
Update compat autodocs.
2018-11-13 21:38:59 -05:00
jab 3a8339d29c OrderedBidict optimizations and code improvements.
Use bidicts for the backing ``_fwdm`` and ``_invm`` mappings,
obviating the need to store key and value data in linked list nodes.

Also drop the unnecessary ^ from the namedtuple identifier regexes.
2018-11-06 16:10:03 -05:00
jab afdf2f4f61 Various improvements.
- Refactor proxied- (i.e. delegated-) to-``_fwdm`` logic
  for better composability and interoperability.

  Drop the ``_Proxied*`` mixin classes
  and instead move their methods
  into :class:`~bidict.BidictBase`,
  which now checks for an object defined by the
  :attr:`~bidict.BidictBase.__delegate__` attribute.
  The :attr:`~bidict.BidictBase.__delegate__` object
  will be delegated to if the method is available on it,
  otherwise a default implementation
  (e.g. inherited from :class:`~collections.abc.Mapping`)
  will be used otherwise.
  Subclasses may set ``__delegate__ = None`` to opt out.

- Consolidate ``_MutableBidict`` into :class:`bidict.bidict`
  now that the dropped mixin classes make it unnecessary.

- Change :attr:`~bidict.BidictBase.__repr_delegate__`
  to take simply a type like :class:`dict` or :class:`list`.

- Upgrade to latest major
  `sortedcontainers <https://github.com/grantjenks/python-sortedcontainers>`__
  version (from v1 to v2)
  for the :ref:`extending:Sorted Bidict Recipes`.

- ``bidict.compat.{view,iter}{keys,values,items}`` on Python2
  no longer assumes the target object implements these methods,
  as they're not actually part of the
  :class:`~collections.abc.Mapping` interface,
  and provides fallback implementations when the methods are unavailable.
  This allows the :ref:`extending:Sorted Bidict Recipes`
  to continue to work with sortedcontainers v2 on Python2.

- Test code in docs via Sphinx rather than pytest.

  Enables running Python version-dependent tests conditionally
  rather than skipping them altogether, as well as hiding import
  statements (via `testsetup`) that otherwise just add noise.

  Run tests (viz. pytest and Sphinx doctest) via a new
  run-tests.py script.
2018-11-05 15:52:59 -05:00
jab 6117d52cc9 increase coverage for OrderedBidictBase.viewkeys() 2018-09-17 20:12:35 -04:00
jab 2aebc46d96 Decrease hypothesis max_examples for daily Travis cron builds
...and suppress the "too_slow" health check for cron and coverage-
enabled builds since it recently started to fail intermittently.
2018-09-09 17:54:56 -04:00
jab 784aacb756 fix some recently changed links 2018-08-19 14:53:20 -04:00
jab 20e1c18af1 update to latest pylint (2.0.0) and appease it 2018-07-16 20:26:36 -04:00
jab 6faf1ef03f Fix updating orderedbidict.inv 2018-04-28 09:50:29 +08:00
jab e049c310bd rename some source files for more clarity 2018-04-26 17:04:02 +07:00
jab 163667d5b4 optimize implementations of keys, values, and items
- Also refactor and improve hypothesis tests.
- Add a test that ordered bidict nodes have no reference cycles.
- Enable coverage on Travis for PyPy 2 now that there is a code path
  that is only taken on PyPy 2 (for FrozenOrderedBidict.iteritems).
- Restore running tests on Travis for all commits, not just for master.
- Docs:
  - OrderedBidict: "What if my Python version has order-preserving dicts?"
  - Equivalent but distinct Hashables: better code example demonstrating
    that this even occurs with a bidict and its own inverse.
2018-04-25 21:34:27 +07:00
jab a818b69aa0 refactor OrderedBidictBase, use weakrefs in nodes 2018-04-22 05:09:36 +07:00
jab cb1d80c97c tweak docs, setup.py, travis, and hypothesis tests
- improve basic usage and "Why can't I just use a dict?" docs
- rename _static -> assets
- use forward declarations of custom rst interpreted text roles we use that
  sphinx defines and that PyPI and GitHub couldn't handle otherwise (e.g.
  :doc:), rather than stripping them out in setup.py
- move __keywords__ and __url__ from setup.py into bidict/metadata.py
- add __version_info__ metadata attribute
- import bidict/metadata.py in setup.py rather than `exec`ing it
- add back Python 3.4 to Travis test matrix
- improve hypothesis settings for max_examples_5000 profile
  (used in nightly CI builds)
- suppress hypothesis's hung_test health check for max_examples_5000 profile
  to prevent flaky tests caused by failed health checks. Avoids errors like:

    E   hypothesis.errors.FailedHealthCheck: Your test has been running for
        at least five minutes. This is probably not what you intended, so by
        default Hypothesis turns it into an error.
    E   See https://hypothesis.readthedocs.io/en/latest/healthchecks.html for
        more information about this. If you want to disable just this health
        check, add HealthCheck.hung_test to the suppress_health_check settings
        for this test.
2018-04-19 15:52:02 +07:00
jab 1a8e11f163 strengthen too-weak assume(..) in test_eq_ne_hash, use "max_examples=5000" for nightly CI jobs, add .readthedocs.yml
The `assume(init_items != init_unequal)` was not sufficient
because it would allow cases like:

    bi_cls=bidict, other_cls=bidict, init_items=[(1, 0), (0, 1)], init_unequal=[(0, 1), (1, 0)]

causing the resulting bidicts' item sets to still be equal,
so assertions like

    assert items_match(some_bidict, other_unequal, relation=ne)

would fail.

Changing the assume to

    assume(set(init_items) != set(init_unequal))

fixes the issue.

Surprisingly, hypothesis only just found a failing example for this
for the first time (in https://travis-ci.org/jab/bidict/jobs/363962257)
despite having numerous opportunities to find it before.
Using a new max_examples=5000 hypothesis profile, only on nightly CI,
should help surface any remaining issues that hypothesis hasn't found yet,
without slowing commit-triggered tests with too much example generation.

Also improve Travis config:
- ditch ALLOW_FAIL=1, for some reason it didn't work
- comment out python 3.7-dev job while Travis is stuck on an old 3.7 alpha
- don't use "- |" command block where we want to see output after each command
- prefix more network-dependent commands with travis_retry

Finally, improve docs building setup:
- Factor out sphinx from "dev" extras_require to its own "docs" extras_require.
- Add a .readthedocs.yml to customize the readthedocs build.
  Use pip and the "docs" extras_require to get an up-to-date Sphinx version.
  Fixes some broken :ref:`addendum:...`  autosectionlabel links in learning-from-bidict.
2018-04-10 11:31:29 +07:00
jab 543d517ad9 bidict.pairs -> _iteritems_args_kw, docs + other small improvements
- improve coverage
- use latest pip on travis
- update to tox 3
- guard Python < 3.3 warning with PYMAJOR == 3 condition
- docs improvements, esp. for viewing on GitHub:
  - fix `foo <bar>`__ link syntax (always use two underscores)
  - use `.. code: python` for code blocks to enable GitHub syntax highlighting
  - ditch `.. include::` which GitHub does not support
  - use sphinx.ext.autosectionlabel
  - only wrap linkcheck with travis_retry
  - s/sibbell/libraries.io
2018-04-06 11:28:44 +07:00
jab 2c031f9cf0 revert back to izip, don't assume pkg_resources available, docs and code tweaks 2018-03-26 20:04:53 +08:00
jab 2f1514907f improve namedbidict base_type docs and some tests 2018-03-02 12:46:59 +11:00
jab 198df4fd5b comments + tests for BidirectionalMapping.inv 2018-03-02 10:11:51 +11:00
jab f2cd4edc82 .compat.izip -> zip, BidirectionalMapping.inv -> abstractproperty, docs, tests 2018-03-01 22:40:53 +11:00