Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use tomllib on Python 3.11+ #1152

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ classifiers = [
dependencies = [
'importlib-metadata>=6.6.0',
'platformdirs>=3.5.1',
'tomli>=2.0.1',
'tomli>=2.0.1; python_version<"3.11"',
]

[project.scripts]
Expand Down
22 changes: 14 additions & 8 deletions yapf/yapflib/file_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ def _GetExcludePatternsFromPyprojectToml(filename):
"""Get a list of file patterns to ignore from pyproject.toml."""
ignore_patterns = []
try:
import tomli as tomllib
Copy link
Contributor

@Spitfire1900 Spitfire1900 Sep 24, 2023

Choose a reason for hiding this comment

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

Not a fan of nested try/catches in favor of https://peps.python.org/pep-0484/#version-and-platform-checking style approach.

I'm also wondering if moving the import to ./yapf/yapflib/__init__.py is a good idea idea vs. conditional login in every file.

Lastly, this package is now part of the package requirements, so managing an ImportError may no longer be applicable.

import sys
if sys.version_info >= (3, 11):
    import tomllib
else:
    import tomli as tomllib

import tomllib
except ImportError:
raise errors.YapfError(
'tomli package is needed for using pyproject.toml as a '
'configuration file')
try:
import tomli as tomllib
except ImportError:
raise errors.YapfError(
'tomli package is needed for using pyproject.toml as a '
'configuration file')

if os.path.isfile(filename) and os.access(filename, os.R_OK):
with open(filename, 'rb') as fd:
Expand Down Expand Up @@ -137,11 +140,14 @@ def GetDefaultStyleForDir(dirname, default_style=style.DEFAULT_STYLE):
else:
with fd:
try:
import tomli as tomllib
import tomllib
except ImportError:
raise errors.YapfError(
'tomli package is needed for using pyproject.toml as a '
'configuration file')
try:
import tomli as tomllib
except ImportError:
raise errors.YapfError(
'tomli package is needed for using pyproject.toml as a '
'configuration file')

pyproject_toml = tomllib.load(fd)
style_dict = pyproject_toml.get('tool', {}).get('yapf', None)
Expand Down
11 changes: 7 additions & 4 deletions yapf/yapflib/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -792,11 +792,14 @@ def _CreateConfigParserFromConfigFile(config_filename):

if config_filename.endswith(PYPROJECT_TOML):
try:
import tomli as tomllib
import tomllib
except ImportError:
raise errors.YapfError(
'tomli package is needed for using pyproject.toml as a '
'configuration file')
try:
import tomli as tomllib
except ImportError:
raise errors.YapfError(
'tomli package is needed for using pyproject.toml as a '
'configuration file')

with open(config_filename, 'rb') as style_file:
pyproject_toml = tomllib.load(style_file)
Expand Down
28 changes: 20 additions & 8 deletions yapftests/file_resources_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,12 @@ def test_get_exclude_file_patterns_from_yapfignore_with_wrong_syntax(self):

def test_get_exclude_file_patterns_from_pyproject(self):
try:
import tomli
import tomllib
except ImportError:
return
try:
import tomli
except ImportError:
return
local_ignore_file = os.path.join(self.test_tmpdir, 'pyproject.toml')
ignore_patterns = ['temp/**/*.py', 'temp2/*.py']
with open(local_ignore_file, 'w') as f:
Expand All @@ -94,9 +97,12 @@ def test_get_exclude_file_patterns_from_pyproject(self):

def test_get_exclude_file_patterns_from_pyproject_no_ignore_section(self):
try:
import tomli
import tomllib
except ImportError:
return
try:
import tomli
except ImportError:
return
local_ignore_file = os.path.join(self.test_tmpdir, 'pyproject.toml')
ignore_patterns = []
open(local_ignore_file, 'w').close()
Expand All @@ -107,9 +113,12 @@ def test_get_exclude_file_patterns_from_pyproject_no_ignore_section(self):

def test_get_exclude_file_patterns_from_pyproject_ignore_section_empty(self):
try:
import tomli
import tomllib
except ImportError:
return
try:
import tomli
except ImportError:
return
local_ignore_file = os.path.join(self.test_tmpdir, 'pyproject.toml')
ignore_patterns = []
with open(local_ignore_file, 'w') as f:
Expand Down Expand Up @@ -177,9 +186,12 @@ def test_setup_config(self):
def test_pyproject_toml(self):
# An empty pyproject.toml file should not be used
try:
import tomli
import tomllib
except ImportError:
return
try:
import tomli
except ImportError:
return

pyproject_toml = os.path.join(self.test_tmpdir, 'pyproject.toml')
open(pyproject_toml, 'w').close()
Expand Down
14 changes: 10 additions & 4 deletions yapftests/style_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,12 @@ def testErrorUnknownStyleOption(self):

def testPyprojectTomlNoYapfSection(self):
try:
import tomli # noqa: F401
import tomllib # noqa: F401
except ImportError:
return
try:
import tomli # noqa: F401
except ImportError:
return

filepath = os.path.join(self.test_tmpdir, 'pyproject.toml')
_ = open(filepath, 'w')
Expand All @@ -242,9 +245,12 @@ def testPyprojectTomlNoYapfSection(self):

def testPyprojectTomlParseYapfSection(self):
try:
import tomli # noqa: F401
import tomllib # noqa: F401
except ImportError:
return
try:
import tomli # noqa: F401
except ImportError:
return

cfg = textwrap.dedent("""\
[tool.yapf]
Expand Down