Skip to content

Fix stdlib/encodings: expose imported submodules#15462

Merged
srittau merged 4 commits intopython:mainfrom
albertedwardson:encodings
Mar 2, 2026
Merged

Fix stdlib/encodings: expose imported submodules#15462
srittau merged 4 commits intopython:mainfrom
albertedwardson:encodings

Conversation

@albertedwardson
Copy link
Contributor

@albertedwardson albertedwardson commented Feb 24, 2026

mypy reports a false positive when accessing encodings.aliases.aliases:

import encodings

def get(key: str) -> str:
    return encodings.aliases.aliases.get(key, key)

reveal_type(encodings.aliases.aliases)

This produces:

main.py:4: error: Returning Any from function declared to return "str"  [no-any-return]
main.py:6: note: Revealed type is "Any"
Found 1 error in 1 file (checked 1 source file)

The revealed type of encodings.aliases.aliases is Any, which causes the dict.get() call to return Any and triggers no-any-return.

At runtime, however, encodings imports certain submodules (including aliases) into its namespace:

Python 3.12.3 (main, Jan 22 2026, 20:57:42) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
Ctrl click to launch VS Code Native REPL
>>> import encodings
>>> dir(encodings)
['CodecRegistryError', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_aliases', '_cache', '_import_tail', '_unknown', 'aliases', 'codecs', 'normalize_encoding', 'search_function', 'sys', 'utf_8']
>>> 

p.s. Actually, utf_8 and utf_8_sig appear at runtime, while only aliases is explicitly imported in __init__.py

@github-actions

This comment has been minimized.

1 similar comment
@github-actions

This comment has been minimized.

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, comment below.

@github-actions
Copy link
Contributor

github-actions bot commented Mar 2, 2026

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@srittau srittau merged commit 35004e9 into python:main Mar 2, 2026
51 checks passed
@albertedwardson albertedwardson deleted the encodings branch March 2, 2026 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants