Title: Fix HasCustomStr to recognise custom __repr__ as a meaningful str (fixes #595) by shivamtiwari3 · Pull Request #653 · google/python-fire · GitHub
Open Graph Title: Fix HasCustomStr to recognise custom __repr__ as a meaningful str (fixes #595) by shivamtiwari3 · Pull Request #653 · google/python-fire
X Title: Fix HasCustomStr to recognise custom __repr__ as a meaningful str (fixes #595) by shivamtiwari3 · Pull Request #653 · google/python-fire
Description: Summary Fixes #595. Objects that override __repr__ but not __str__ (e.g. datasets.Dataset from HuggingFace) were shown as a help screen instead of being printed, because HasCustomStr only checked for a custom __str__. Problem Python's object.__str__ delegates to __repr__, so a class that defines a custom __repr__ already produces a meaningful custom string via str(obj) — even without defining __str__. However, HasCustomStr in value_types.py only inspected __str__: str_attr = class_attrs.get('__str__') if str_attr and str_attr.defining_class is not object: return True # __repr__-only classes fall through → False → help screen shown This caused fire.Fire(main) to show the datasets.Dataset help screen rather than the dataset's repr string. Solution After the existing __str__ check, also look for a custom __repr__ — but only on user-defined (non-stdlib) types, so that Python built-in containers (dict, list, OrderedDict, …) continue to be treated as navigable groups rather than values. The guard uses sys.stdlib_module_names (Python ≥ 3.10) and falls back to a minimal sentinel set for older interpreters: _STDLIB_MODULE_NAMES = frozenset( getattr(sys, 'stdlib_module_names', {'builtins', 'collections', 'abc'}) ) If the __repr__-defining class's top-level module is not in that set, the object is treated as a value. Testing Added to test_components.py: ClassWithCustomRepr — overrides __repr__ only ClassWithCustomStr — overrides __str__ only (existing behaviour) fn_returning_custom_repr / fn_returning_custom_str — functions returning those objects Added to core_test.py: testPrintObjectWithCustomStr — existing __str__ path still works testPrintObjectWithCustomRepr — new __repr__ path: output is ClassWithCustomRepr(), not a help screen Run command: python3 -m pytest fire/ --ignore=fire/docstrings_fuzz_test.py --ignore=fire/parser_fuzz_test.py 262 tests pass, 0 failures. Checklist Fixes the reported issue Existing behaviour unchanged (no regressions — dict/list/OrderedDict still show help screens) Tests added All 262 tests pass Code style matches project conventions
Open Graph Description: Summary Fixes #595. Objects that override __repr__ but not __str__ (e.g. datasets.Dataset from HuggingFace) were shown as a help screen instead of being printed, because HasCustomStr only checked f...
X Description: Summary Fixes #595. Objects that override __repr__ but not __str__ (e.g. datasets.Dataset from HuggingFace) were shown as a help screen instead of being printed, because HasCustomStr only checked f...
Opengraph URL: https://github.com/google/python-fire/pull/653
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:6244c7e6-6098-6ae5-7585-79c22a81c49a |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 9C58:16A191:31D500:4312A5:6A51BEE0 |
| html-safe-nonce | 96a4db95d07f6d293270201efeb26fc78ce4d694b3da270faf5516ef628777e9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QzU4OjE2QTE5MTozMUQ1MDA6NDMxMkE1OjZBNTFCRUUwIiwidmlzaXRvcl9pZCI6Ijg2MjUwNjc0NzM1MDk2NjI0MzIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | aa41cfddb26e7d5ab1e7f151530268e26013d9c4f73a477c7bb4571a96e01120 |
| hovercard-subject-tag | pull_request:3379894469 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/google/python-fire/pull/653/files |
| twitter:image | https://avatars.githubusercontent.com/u/33183708?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/33183708?s=400&v=4 |
| og:image:alt | Summary Fixes #595. Objects that override __repr__ but not __str__ (e.g. datasets.Dataset from HuggingFace) were shown as a help screen instead of being printed, because HasCustomStr only checked f... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/google/python-fire git https://github.com/google/python-fire.git |
| octolytics-dimension-user_id | 1342004 |
| octolytics-dimension-user_login | |
| octolytics-dimension-repository_id | 82729529 |
| octolytics-dimension-repository_nwo | google/python-fire |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 82729529 |
| octolytics-dimension-repository_network_root_nwo | google/python-fire |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 7aed05249554b889eb33d002851a973eebcc7e91 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width