You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Wrapping basic types doesn't work as expected with the standard library #174
I'm experiencing some issues wrapping base types such as str. For example, the str.join() fails with "expected str instance" which doesn't happen when str is inherited directly.
I've experienced the same issue with the re module ('expected string or bytes-like object"). I've also experienced it when wrapping int. For example, datetime.timedelta fails with "TypeError: unsupported type for timedelta days component" when using a proxy object.
Workaround
Currently, the workaround is to convert it to the appropriate type:
print(",".join(map(str, [proxy1])))
# proxy1
However, it would be great if this wasn't needed as other developers on the project should be able to use the proxy without knowing it's a proxy. We use the proxy to store some information about the underlying data.
The problem with anything implemented as a C function is that for where they expect to deal with builtin type they will often have explicit checks for the exact type (not even isinstance() equivalent) and nothing else will work. There isn't a way around it.
Why are you inheriting from builtin types anyway?
For primitive literal types (eg., string, integers, floats) inheriting them from them isn't necessarily a good idea.
I didn't plan to, I included it for comparison purposes. However, I'm now considering it since the type (either string or proxy) is not known in advance and they should be interchangeable.
Going to close out this issue as there isn't really a solution for wrapping basic types in Python. For some things it may be possible, but not for many other things.
Activity
GrahamDumpleton commentedon Mar 29, 2021
The problem with anything implemented as a C function is that for where they expect to deal with builtin type they will often have explicit checks for the exact type (not even isinstance() equivalent) and nothing else will work. There isn't a way around it.
Why are you inheriting from builtin types anyway?
For primitive literal types (eg., string, integers, floats) inheriting them from them isn't necessarily a good idea.
rnikiforova commentedon Mar 30, 2021
:-(
I didn't plan to, I included it for comparison purposes. However, I'm now considering it since the type (either string or proxy) is not known in advance and they should be interchangeable.
GrahamDumpleton commentedon Aug 4, 2021
Going to close out this issue as there isn't really a solution for wrapping basic types in Python. For some things it may be possible, but not for many other things.