Coder Social home page Coder Social logo

Comments (3)

maximlt avatar maximlt commented on June 11, 2024 1

We could improve the error message here. The error happens while creating the child class when it detects that the type of a Parameter has changed. In that case we could catch the validation error (a mix of ValueError, TypeError OSError) and enhance it with some more context.

from param.

jbednar avatar jbednar commented on June 11, 2024

Thanks for raising this issue; we definitely need to document this better! Behavior in this case is intentionally undefined because this is not supported code. Subclasses should never have parameters with a fundamentally different type than a superclass parameter of the same name. I believe there is an open issue about detecting that usage and warning or raising an error, though I can't find it just now.

For background, properly structured Python subclasses should satisfy the "is a" relationship: Any code that accepts type parent should be happy to get type child, because here child has been declared to be a (sub)type of parent (it inherits the interface of parent). See the definition of is_instance in the Python docs for the semantics involved. Here, c is (also) an instance of type parent, even though it's been declared as child, which for this code is a problem because a child does not satisfy the interface of the parent class:

image

Code written for parent will expect to see an integer value for p, but here it will be a list, immediately causing bugs.

That doesn't mean that you can't change types across the inheritance hierarchy. E.g. you can have a Number parameter in the parent become an Integer parameter in the child, since any valid integer value is also a valid number. But you can't have the same parameter be an Integer in the parent and a Number in the child, because then if someone provides an instance of the child to code written for the parent (which is fully legal by Python typing rules, since a child here "is a" (specific instance of) parent), that code will find arbitrary numbers where only Integers were declared to be allowed. Param is designed to avoid such surprises, so don't write code that causes them!

Changing an Integer to a List is even less appropriate, because code written for an Integer will essentially never work if it finds a List. So either don't have child inherit from parent (e.g. have them both inherit the truly shared bits from a third class), or change the name of p in the child so that it is not purporting to be the same parameter p as in the parent, or otherwise rewrite the code to avoid this error.

from param.

samimia-swks avatar samimia-swks commented on June 11, 2024

I see. Thanks for the detailed explanation.

from param.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.