Skip to content

Tests for what kind of an element something is should test the parent #803

Closed

Description

@roed314

Calls like:

algebras/algebra_order.py:        elif isinstance(x, RingElement) and x in self.base_ring():
algebras/algebra_order_ideal.py:        elif isinstance(x, RingElement) and x in self.base_ring():
algebras/free_algebra_quotient_element.py:        elif isinstance(x, RingElement) and not isinstance(x, AlgebraElement) and x in R:
rings/infinity.py:        elif isinstance(x, RingElement) or isinstance(x, (int,long,float,complex)):
rings/infinity.py:        elif isinstance(x, RingElement):

should actually be checking to see if the parents are of the appropriate type. The element types are not always reliable: parents more accurately reflect the mathematical structure (mostly because they can have multiple inheritance).

There may be more instances in addition to those above (I just ran search_src("isinstance(x, RingElement)"))

Component: basic arithmetic

Reviewer: Vincent Delecroix

Issue created by migration from https://trac.sagemath.org/ticket/803

Activity

  1. added this to the sage-5.11 milestone on Oct 3, 2007
  2. williamstein commented on Feb 25, 2008

    @williamstein
    Contributor
    comment:1

    I'm dubious:

    My immediate thought when looking at #803 is that it is the wrong
    idea in the first place. If I had looked at #803 before
    now I would have considered marking it "invalid".

    There are individual instances that involve "isinstance" that were
    perhaps badly written. E.g., the first example:

            elif isinstance(x, RingElement) and x in self.base_ring():
                return True
    

    Here I think the original author (David Kohel) may have written
    this at a time when "in" could raise an exception if x isn't
    a RingElement. That's no longer the case, so in this particular
    example the right fix is I think to put:

            elif x in self.base_ring():
                return True
    

    In probably hundreds of other cases the use of isinstance in
    Sage is exactly right. In some cases it could be improved,
    but how will depend in each case on understanding the code.

  3. modified the milestones: sage-5.11, sage-5.12 on Aug 13, 2013
  4. removed this from the sage-6.4 milestone on Apr 13, 2015
  5. videlec commented on Apr 14, 2015

    @videlec
    Contributor

    Reviewer: Vincent Delecroix

  6. added 2 commits that reference this issue on Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions