-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Teach inpsect.getdoc() to read __slots__ with an optional data dictionary #80507
Comments
The __slots__ variable already works with dictionaries. The values are simply ignored. I propose teaching help() to read those optional dictionaries to give better information on member objects (much like we already have with property objects). This is inspired by data dictionaries for database tables. The pydoc implementation would be somewhat easy. Roughly this: for name in data_descriptors:
print(f' | {name}'
if isinstance(slots, dict) and name in slots:
print(f' | {slots[name]}')
print(' |') ==== Example ====================================================
__slots__ = dict(
category = 'Primary use: road, cross-over, or hybrid',
model = 'Unique six digit vendor-supplied code',
size = 'Rider size: child, small, medium, large, extra-large',
price = 'Manufacturer suggested retail price',
) >>> help(Bicycle)
Help on class Bicycle in module __main__: class Bicycle(builtins.object)
| Data descriptors defined here:
|
| category
| Primary use: road, cross-over, or hybrid
|
| model
| Unique six digit vendor-supplied code
|
| price
| Rider size: child, small, medium, large, extra-large
|
| size
| Manufacturer suggested retail price |
I am not sure that this is the best application of dict as __slots__. Maybe use dict for specifying default values? Currently slots are not compatible with class-level values used as fallbacks. Following the pattern for namedtuple attributes, docstrings for slots could be specified as: class Bicycle:
__slots__ = 'category', 'model', 'size', 'price'
Bicycle.category.__doc__ = 'Primary use: road, cross-over, or hybrid'
Bicycle.model.__doc__ = 'Unique six digit vendor-supplied code'
Bicycle.size.__doc__ = 'Rider size: child, small, medium, large, extra-large'
Bicycle.price.__doc__ = 'Manufacturer suggested retail price' |
The direct assignments to __doc__ are reasonable for named tuples because there usually isn't any code between the factory function call and the __doc__ assignments. For other classes, the technique is awkward because it widely separates the initial field name iterable from the corresponding docstrings. Setting default values is responsibility of the __new__ or __init__ methods. It doesn't make sense to use a __slots__ dictionary for this purpose as well. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: