Skip to content

Replace deprecated codecs.open by open#223

Open
FriedrichFroebel wants to merge 2 commits intomicroformats:mainfrom
FriedrichFroebel:codecs-open
Open

Replace deprecated codecs.open by open#223
FriedrichFroebel wants to merge 2 commits intomicroformats:mainfrom
FriedrichFroebel:codecs-open

Conversation

@FriedrichFroebel
Copy link
Copy Markdown

Running tests for one of my packages relying on mf2py with warnings enabled prints:

../../../../../opt/hostedtoolcache/Python/3.14.0/x64/lib/python3.14/site-packages/mf2py/backcompat.py:50: 11 warnings
  /opt/hostedtoolcache/Python/3.14.0/x64/lib/python3.14/site-packages/mf2py/backcompat.py:50: DeprecationWarning: codecs.open() is deprecated. Use open() instead.
    with codecs.open(file_path, "r", "utf-8") as f:

file_path = os.path.join(_RULES_LOC, filename)
root = os.path.splitext(filename)[0]
with codecs.open(file_path, "r", "utf-8") as f:
with open(file_path, "r", "utf-8") as f:
Copy link
Copy Markdown

@ysamlan ysamlan Mar 13, 2026

Choose a reason for hiding this comment

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

FYI, Python 3 open() takes encoding as a keyword argument, not positional. The third positional
parameter is buffering (expects an int), so this:

open(file_path, "r", "utf-8")

raises TypeError: 'str' object cannot be interpreted as an integer at import time on Python 3.14. Should be:

open(file_path, encoding="utf-8") ('r' is the default for mode, so that's optional).

> help(open)
Help on built-in function open in module _io:
open(
    file,
    mode='r',
    buffering=-1,
    encoding=None,
    errors=None,
    newline=None,
    closefd=True,
    opener=None
)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks, I did not test the change back then. I have made all the optional parameters keywords.

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