self as property name :(
Closed this issue · 3 comments
CosmicAnalogue465 commented
Hi,
If a property name is called self, the object cannot be instanciated :s - See example code below
import python_jsonschema_objects as pjs
schema = {
'properties': {
'self': {
'additionalProperties': False,
'properties': {
'href': {
'type': 'string'
}
},
'required': ['href'],
'title': 'link_field',
'type': 'object'
}
},
'required': ['self'],
'title': 'LinkObject',
'type': 'object'
}
ns = pjs.ObjectBuilder(schema).build_classes()
link = ns.Linkobject(**{"self": {"href": "Resource/123"}})
This raises the exception during the instanciation of the LinkObject:
TypeError: __init__() got multiple values for argument 'self'
Is this fixable somehow ?
cwacek commented
Oh that’s tough. I suppose the workaround is to change the “self” reference variable name, but I see two problems with that:
1. It’s very anti-pythonic
2. Anything it gets changed to also has a name collision possibility.
… On Apr 23, 2020, at 3:20 AM, Vincent c ***@***.***> wrote:
Hi,
If a property name is called self, the object cannot be instanciated :s - See example code below
import python_jsonschema_objects as pjs
schema = {
'properties': {
'self': {
'additionalProperties': False,
'properties': {
'href': {
'type': 'string'
}
},
'required': ['href'],
'title': 'link_field',
'type': 'object'
}
},
'required': ['self'],
'title': 'LinkObject',
'type': 'object'
}
ns = pjs.ObjectBuilder(schema).build_classes()
link = ns.Linkobject(**{"self": {"href": "Resource/123"}})```
This raises the exception :
```TypeError: __init__() got multiple values for argument 'self'```
Is this fixable somehow ?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
st31ny commented
I could imagine to store all attributes in a dict and allow property access through __getattr__
. That way all names should be possible for attributes.
github-actions commented
Stale issue message