gh-68451: Fix unittest discovery to support Unicode module names#144853
gh-68451: Fix unittest discovery to support Unicode module names#144853RoryGlenn wants to merge 1 commit intopython:mainfrom
Conversation
Replace the ASCII-only VALID_MODULE_NAME regex with str.isidentifier() to support test modules whose names start with non-ASCII Unicode letters (e.g., café.py, 測試.py). Also add a directory name validation check so that directories with invalid identifier names (e.g., containing hyphens) are skipped during package discovery.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3ac73104fa
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| # gh-68451: use str.isidentifier() to support Unicode module names, | ||
| # rather than a restrictive ASCII-only regex. | ||
| root, ext = os.path.splitext(path) | ||
| return ext == '.py' and root.isidentifier() |
There was a problem hiding this comment.
Preserve case-insensitive .py matching in module filter
This new check makes extension validation case-sensitive (ext == '.py'), so files like test_example.PY are now silently skipped even when discovery patterns match them (notably on Windows/case-insensitive filesystems where fnmatch('test*.py') can match test_example.PY). The previous regex was case-insensitive, so this change regresses discovery for existing suites that use uppercase/mixed-case .py extensions.
Useful? React with 👍 / 👎.
gh-68451: Fix unittest discovery to support Unicode module names
Summary
unittesttest discovery previously used an ASCII-only regex ([_a-z]\w*\.py$) to validate module names, which rejected test files starting with non-ASCII Unicode letters (e.g.,café.py,測試.py). This PR replaces the regex withstr.isidentifier(), which correctly handles all valid Python identifiers per PEP 3131.Changes
Lib/unittest/loader.pyVALID_MODULE_NAMEregex and the unusedimport re_valid_module_name()function that usesos.path.splitext()+str.isidentifier()to validate module filenames_find_test_path()— directories with invalid identifier names (e.g., containing hyphens) are now properly skipped during package discoveryLib/test/test_unittest/test_discovery.pytest_valid_module_name: Tests the new_valid_module_name()function with ASCII, Unicode, and invalid namestest_find_tests_with_unicode_modules: Integration test verifying Unicode-named modules are discovered alongside ASCII onestest_find_test_path_rejects_invalid_dir_name: Tests that directories with invalid identifier names are skippedPrior work
This issue has two stale PRs (#1338 from 2017, #13149 from 2019) that were never merged. This PR incorporates reviewer feedback from those PRs:
str.isidentifier()instead of regex (per @vstinner, @ezio-melotti)self.addCleanup(setattr, ...)pattern (per @ezio-melotti)Testing
All 1,095 unittest tests pass.