Title: Fix leading space in surnames after capitalize() with empty middle name by patchwright · Pull Request #164 · derek73/python-nameparser · GitHub
Open Graph Title: Fix leading space in surnames after capitalize() with empty middle name by patchwright · Pull Request #164 · derek73/python-nameparser
X Title: Fix leading space in surnames after capitalize() with empty middle name by patchwright · Pull Request #164 · derek73/python-nameparser
Description: Summary After capitalize() is called on a name with any absent attribute, the corresponding *_list attribute becomes [''] instead of []. The most visible symptom is a spurious leading space in surnames (e.g. ' Doe' instead of 'Doe'). The same class of bug also affects suffix_list. Reproduction from nameparser import HumanName n = HumanName('john doe') n.capitalize() print(repr(n.surnames)) # ' Doe' <- leading space print(n.middle_list) # [''] <- should be [] print(n.title_list) # [''] <- should be [] print(n.suffix_list) # [''] <- should be [] (no suffix present) The leading space is a real observable artifact, not just an empty-list detail: downstream code that joins or compares surnames silently breaks. Cause cap_piece(...) returns '' for an absent name part. Splitting an empty string with an explicit separator always preserves a spurious empty token: ''.split(' ') → [''] ''.split(', ') → [''] whereas str.split() with no argument returns [] for an empty string. Fix Whitespace-delimited attributes (title, first, middle, last) Drop the explicit separator — bare .split() correctly returns [] for an empty string: - self.title_list = self.cap_piece(self.title, 'title').split(' ') - self.first_list = self.cap_piece(self.first, 'first').split(' ') - self.middle_list = self.cap_piece(self.middle, 'middle').split(' ') - self.last_list = self.cap_piece(self.last, 'last').split(' ') + self.title_list = self.cap_piece(self.title, 'title').split() + self.first_list = self.cap_piece(self.first, 'first').split() + self.middle_list = self.cap_piece(self.middle, 'middle').split() + self.last_list = self.cap_piece(self.last, 'last').split() Suffix suffix_list must keep its comma delimiter (suffixes are stored as "Ph.D., M.D."), so bare .split() can't be used. Instead, filter empty tokens after splitting: - self.suffix_list = self.cap_piece(self.suffix, 'suffix').split(', ') + self.suffix_list = [s for s in self.cap_piece(self.suffix, 'suffix').split(', ') if s] Tests Split the original regression test into three focused methods (normal path, force=True path, list invariants) Added test_capitalize_title_and_last_only_no_spurious_tokens — exercises empty first_list and middle_list simultaneously Added three suffix tests: empty suffix → [], single suffix, multiple suffixes Full suite: 686 tests OK (20 expected failures).
Open Graph Description: Summary After capitalize() is called on a name with any absent attribute, the corresponding *_list attribute becomes [''] instead of []. The most visible symptom is a spurious leading space...
X Description: Summary After capitalize() is called on a name with any absent attribute, the corresponding *_list attribute becomes [''] instead of []. The most visible symptom is a spurious leadi...
Opengraph URL: https://github.com/derek73/python-nameparser/pull/164
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:90a99534-d72a-dd3e-a123-ce43f76e2eba |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 9B10:1A7343:2BBDA7:3AE021:6A554E0C |
| html-safe-nonce | 18f80e5c03fe1ffdafd4cc1f78bfe286b4e8d12a74e12929a6a0b7e4ae53ed15 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QjEwOjFBNzM0MzoyQkJEQTc6M0FFMDIxOjZBNTU0RTBDIiwidmlzaXRvcl9pZCI6IjgzOTcxMzU5NzYwNTM2NzMwOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 09e1f4295cc13b68dc4b9b08ac4d6940c237a9c042bf9876314aaf9e08450aba |
| hovercard-subject-tag | pull_request:3863581573 |
| 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/derek73/python-nameparser/pull/164/files |
| twitter:image | https://avatars.githubusercontent.com/u/292882882?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/292882882?s=400&v=4 |
| og:image:alt | Summary After capitalize() is called on a name with any absent attribute, the corresponding *_list attribute becomes [''] instead of []. The most visible symptom is a spurious leading space... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | cd9eea424140838b22e9eabd53d92b5702617ddd6d76efcd3a37f08d44309ca5 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/derek73/python-nameparser git https://github.com/derek73/python-nameparser.git |
| octolytics-dimension-user_id | 18306 |
| octolytics-dimension-user_login | derek73 |
| octolytics-dimension-repository_id | 18353524 |
| octolytics-dimension-repository_nwo | derek73/python-nameparser |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 18353524 |
| octolytics-dimension-repository_network_root_nwo | derek73/python-nameparser |
| 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 | 3084b9f587e8f1e19803bbe0c77ac77ce3c99c5c |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width