Ticket #13688: trac_13688-finite_sets_cardinality_override-ts.patch

File trac_13688-finite_sets_cardinality_override-ts.patch, 3.5 KB (added by Travis Scrimshaw, 10 years ago)
  • sage/categories/finite_enumerated_sets.py

    # HG changeset patch
    # User Travis Scrimshaw <tscrim@ucdavis.edu>
    # Date 1360990290 28800
    # Node ID 36485e4d0efd65645ce0405f4dadd0e66bbf4efb
    # Parent 6fd86fa95cdaa12dd489c25d6c6554ecf139a351
    Trac #13688 - Allow finite sets cardinality to handle optional args.
    
    diff --git a/sage/categories/finite_enumerated_sets.py b/sage/categories/finite_enumerated_sets.py
    a b class FiniteEnumeratedSets(Category): 
    9696            """
    9797            return True
    9898
    99         def _cardinality_from_iterator(self):
     99        def _cardinality_from_iterator(self, *ignored_args, **ignored_kwds):
    100100            """
    101101            The cardinality of ``self``.
    102102
    class FiniteEnumeratedSets(Category): 
    125125            TESTS:
    126126
    127127            This method shall return an ``Integer``; we test this
    128             here, because :meth:'_test_enumerated_set_iter_cardinality'
     128            here, because :meth:`_test_enumerated_set_iter_cardinality`
    129129            does not do it for us::
    130130
    131131                sage: type(C._cardinality_from_iterator())
    132132                <type 'sage.rings.integer.Integer'>
     133
     134            We ignore additional inputs since during doctests classes which
     135            override ``cardinality()`` call up to the category rather than
     136            their own ``cardinality()`` method (see :trac:`13688`)::
     137
     138                sage: C = FiniteEnumeratedSets().example()
     139                sage: C._cardinality_from_iterator(algorithm='testing')
     140                3
     141
     142            Here is a more complete example::
     143
     144                sage: class TestParent(Parent):
     145                ...     def __init__(self):
     146                ...         Parent.__init__(self, category=FiniteEnumeratedSets())
     147                ...     def __iter__(self):
     148                ...         yield 1
     149                ...         return
     150                ...     def cardinality(self, dummy_arg):
     151                ...         return 1 # we don't want to change the semantics of cardinality()
     152                sage: P = TestParent()
     153                sage: P.cardinality(-1)
     154                1
     155                sage: v = P.list(); v
     156                [1]
     157                sage: P.cardinality()
     158                1
     159                sage: P.cardinality('use alt algorithm') # Used to break here: see :trac:`13688`
     160                1
     161                sage: P.cardinality(dummy_arg='use alg algorithm') # Used to break here: see :trac:`13688`
     162                1
    133163            """
    134164            c = 0
    135165            for _ in self:
    class FiniteEnumeratedSets(Category): 
    156186            """
    157187            return [x for x in self]
    158188
    159         def _cardinality_from_list(self):
     189        def _cardinality_from_list(self, *ignored_args, **ignored_kwds):
    160190            """
    161191            The cardinality of ``self``.
    162192
    class FiniteEnumeratedSets(Category): 
    170200                sage: C = FiniteEnumeratedSets().example()
    171201                sage: C._cardinality_from_list()
    172202                3
     203
     204            We ignore additional inputs since during doctests classes which
     205            override ``cardinality()`` call up to the category rather than
     206            their own ``cardinality()`` method (see :trac:`13688`)::
     207
     208                sage: C = FiniteEnumeratedSets().example()
     209                sage: C._cardinality_from_list(algorithm='testing')
     210                3
    173211            """
    174212            # We access directly the cache self._list to bypass the
    175213            # copy that self.list() currently does each time.