Comments (3)
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.
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:
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.
I see. Thanks for the detailed explanation.
from param.
Related Issues (20)
- Default range of param.Range fails to update using self.param.<parameter>.default assignment HOT 4
- Ability to apply @param.depends to all (nested) Parameters of a class HOT 1
- Param with bounds should default to lower bound HOT 7
- Add .is_active parameter to reactive expressions HOT 3
- Let reactive expressions .watch method support async functions
- Context manager .param.update should restore references
- Let pn.Param accept a list of objects HOT 1
- Warn on errors thru rx updates
- Make param.depends efficient HOT 6
- Parameter called outputs does not work with `.rx` HOT 4
- Triggering a function in one instance based on parameter changes in another HOT 2
- Initialization changes constant attribute. HOT 3
- Missing `param_help.png` in `User Guide/Parameters`
- Discussion about the intro video HOT 1
- Should methods on Parameterizeds be treated as references by default HOT 3
- `name` of a Parameterized instance overridden when is the default of a `readonly/constant` Parameter HOT 2
- Error on printing rx
- RX: output not updated when indexing a DataFrame with value_start/value_end HOT 1
- Small text issue in doc Getting Started
- Ability to specify multiple classes in class_ for ClassSelector parameter type needs to be documented
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from param.