Title: Forbid `functools.singledispatch` / `singledispatchmethod` via Ruff banned-api by Copilot · Pull Request #1361 · usethis-python/usethis-python · GitHub
Open Graph Title: Forbid `functools.singledispatch` / `singledispatchmethod` via Ruff banned-api by Copilot · Pull Request #1361 · usethis-python/usethis-python
X Title: Forbid `functools.singledispatch` / `singledispatchmethod` via Ruff banned-api by Copilot · Pull Request #1361 · usethis-python/usethis-python
Description: functools.singledispatch has poor static analysis semantics. This PR bans it via Ruff's flake8-tidy-imports.banned-api and replaces all existing usages with explicit isinstance-based if-branch dispatch.
Config
Added to pyproject.toml:
lint.flake8-tidy-imports.banned-api."functools.singledispatch".msg = "Use if-branch isinstance logic instead of singledispatch."
lint.flake8-tidy-imports.banned-api."functools.singledispatchmethod".msg = "Use if-branch isinstance logic instead of singledispatchmethod."
Refactoring pattern
All @singledispatch / @singledispatchmethod usages replaced:
# Before
@singledispatch
def _as_dict(value: INIDocument | Section) -> ...:
raise NotImplementedError
@_as_dict.register(INIDocument)
def _(value: INIDocument) -> ...:
return {k: _as_dict(v) for k, v in value.items()}
@_as_dict.register(Section)
def _(value: Section) -> ...:
return {option.key: option.value for option in value.iter_options()}
# After
def _as_dict(value: INIDocument | Section) -> ...:
if isinstance(value, INIDocument):
return {k: _as_dict(v) for k, v in value.items()}
elif isinstance(value, Section):
return {option.key: option.value for option in value.iter_options()}
else:
assert_never(value)
Files changed
_file/ini/io_.py — _as_dict
_pipeweld/func.py — Adder.partition_component (method) and _get_instructions_for_insertion; complex branches extracted into _partition_series_component, _partition_parallel_component, _partition_depgroup_component
_integrations/ci/bitbucket/pipeweld.py — get_pipeweld_object, _extract_step_from_item, _insert_parallel_step, _is_insertion_necessary; extracted _extract_step_from_parallel_item
_integrations/ci/bitbucket/steps.py — _censor_step, get_steps_in_pipeline_item; extracted _censor_parallel_item_step, _censor_stage_item_step
_integrations/pydantic/dump.py — fancy_model_dump; extracted _fancy_model_dump_list, _fancy_model_dump_dict, _fancy_model_dump_base_model
Where functions were large, helpers were extracted to stay within Ruff's branch/return-count limits (PLR0912, PLR0911). All dispatch exhaustiveness is enforced via assert_never.
Original prompt
This section details on the original issue you should resolve
Open Graph Description: functools.singledispatch has poor static analysis semantics. This PR bans it via Ruff's flake8-tidy-imports.banned-api and replaces all existing usages with explicit isinstance-based if-branch ...
X Description: functools.singledispatch has poor static analysis semantics. This PR bans it via Ruff's flake8-tidy-imports.banned-api and replaces all existing usages with explicit isinstance-based if-bra...
Opengraph URL: https://github.com/usethis-python/usethis-python/pull/1361
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:1733505d-e6d8-555e-8928-96e301b174a4 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | B970:326119:51025:6D905:6A4D7902 |
| html-safe-nonce | 5f0a51df4b485797371b804f05fb704261ffab28f30b6e892b68a8698cfda5bb |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCOTcwOjMyNjExOTo1MTAyNTo2RDkwNTo2QTRENzkwMiIsInZpc2l0b3JfaWQiOiIxMzY1NDA2NDc5MjU5ODkxOTcwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 7b2374aa62cd54ee12d48a1ea5e0e161e1364e536e97ee6b04384f2e72e18150 |
| hovercard-subject-tag | pull_request:3381478063 |
| 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/usethis-python/usethis-python/pull/1361/files |
| twitter:image | https://avatars.githubusercontent.com/in/1143301?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/in/1143301?s=400&v=4 |
| og:image:alt | functools.singledispatch has poor static analysis semantics. This PR bans it via Ruff's flake8-tidy-imports.banned-api and replaces all existing usages with explicit isinstance-based if-branch ... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7f8e15305e2ab58890ca84b830615e375f5df303cf471cafcba0c08272981be3 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/usethis-python/usethis-python git https://github.com/usethis-python/usethis-python.git |
| octolytics-dimension-user_id | 216362695 |
| octolytics-dimension-user_login | usethis-python |
| octolytics-dimension-repository_id | 842189705 |
| octolytics-dimension-repository_nwo | usethis-python/usethis-python |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 842189705 |
| octolytics-dimension-repository_network_root_nwo | usethis-python/usethis-python |
| 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 | 8608b0750fc35e283e5ec5b094bff942d7a0e197 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width