I am working with a C++ library wrapped for Python.
The library defines classes, each with many attributes that are like Python standard types but with a few extra methods. eg in the Python stubs “String” inherits from the standard “str”
class String(str):
def get_key(self) -> str:
"""Return the key for this property."""
...
and then a class might have attributes of type “String”, eg
class Asset:
name: String = ''
...
The C++ code supports getting and setting these attribute values using regular Python types. So I can do
my_asset.name = 'Foo'
print(my_asset.name)
However Python type-checking (Pylance in this case) doesn’t like the first line above. It says
Cannot assign to attribute “name” for class “Asset”
“str” is incompatible with “String” PylancereportAttributeAccessIssue
(variable) name: str
Is there a way I can modify my Python stubs so that type-checkers will understand that String
is compatible with str
, not just that in inherits from it?