Skip to content
Merged
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 setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@ def run(self):
'test': RunTests,
},
extras_require={
'pyproject': ['toml'],
'pyproject': ['tomli'],
},
)
15 changes: 8 additions & 7 deletions yapf/yapflib/file_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,15 @@ def _GetExcludePatternsFromPyprojectToml(filename):
"""Get a list of file patterns to ignore from pyproject.toml."""
ignore_patterns = []
try:
import toml
import tomli as tomllib
except ImportError:
raise errors.YapfError(
"toml package is needed for using pyproject.toml as a "
"tomli package is needed for using pyproject.toml as a "
"configuration file")

if os.path.isfile(filename) and os.access(filename, os.R_OK):
pyproject_toml = toml.load(filename)
with open(filename, 'rb') as fd:
pyproject_toml = tomllib.load(fd)
ignore_patterns = pyproject_toml.get('tool',
{}).get('yapfignore',
{}).get('ignore_patterns', [])
Expand Down Expand Up @@ -127,19 +128,19 @@ def GetDefaultStyleForDir(dirname, default_style=style.DEFAULT_STYLE):
# See if we have a pyproject.toml file with a '[tool.yapf]' section.
config_file = os.path.join(dirname, style.PYPROJECT_TOML)
try:
fd = open(config_file)
fd = open(config_file, 'rb')
except IOError:
pass # It's okay if it's not there.
else:
with fd:
try:
import toml
import tomli as tomllib
except ImportError:
raise errors.YapfError(
"toml package is needed for using pyproject.toml as a "
"tomli package is needed for using pyproject.toml as a "
"configuration file")

pyproject_toml = toml.load(config_file)
pyproject_toml = tomllib.load(fd)
style_dict = pyproject_toml.get('tool', {}).get('yapf', None)
if style_dict is not None:
return config_file
Expand Down
25 changes: 14 additions & 11 deletions yapf/yapflib/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,17 +746,18 @@ def _CreateConfigParserFromConfigFile(config_filename):
# Provide a more meaningful error here.
raise StyleConfigError(
'"{0}" is not a valid style or file path'.format(config_filename))
with open(config_filename) as style_file:
config = py3compat.ConfigParser()
if config_filename.endswith(PYPROJECT_TOML):
try:
import toml
except ImportError:
raise errors.YapfError(
"toml package is needed for using pyproject.toml as a "
"configuration file")

pyproject_toml = toml.load(style_file)
config = py3compat.ConfigParser()

if config_filename.endswith(PYPROJECT_TOML):
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)
style_dict = pyproject_toml.get("tool", {}).get("yapf", None)
if style_dict is None:
raise StyleConfigError(
Expand All @@ -766,7 +767,9 @@ def _CreateConfigParserFromConfigFile(config_filename):
config.set('style', k, str(v))
return config

with open(config_filename) as style_file:
config.read_file(style_file)

if config_filename.endswith(SETUP_CONFIG):
if not config.has_section('yapf'):
raise StyleConfigError(
Expand Down
10 changes: 5 additions & 5 deletions yapftests/file_resources_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def test_get_exclude_file_patterns_from_yapfignore_with_wrong_syntax(self):

def test_get_exclude_file_patterns_from_pyproject(self):
try:
import toml
import tomli
except ImportError:
return
local_ignore_file = os.path.join(self.test_tmpdir, 'pyproject.toml')
Expand All @@ -93,7 +93,7 @@ def test_get_exclude_file_patterns_from_pyproject(self):
@unittest.skipUnless(py3compat.PY36, 'Requires Python 3.6')
def test_get_exclude_file_patterns_from_pyproject_with_wrong_syntax(self):
try:
import toml
import tomli
except ImportError:
return
local_ignore_file = os.path.join(self.test_tmpdir, 'pyproject.toml')
Expand All @@ -109,7 +109,7 @@ def test_get_exclude_file_patterns_from_pyproject_with_wrong_syntax(self):

def test_get_exclude_file_patterns_from_pyproject_no_ignore_section(self):
try:
import toml
import tomli
except ImportError:
return
local_ignore_file = os.path.join(self.test_tmpdir, 'pyproject.toml')
Expand All @@ -122,7 +122,7 @@ 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 toml
import tomli
except ImportError:
return
local_ignore_file = os.path.join(self.test_tmpdir, 'pyproject.toml')
Expand Down Expand Up @@ -192,7 +192,7 @@ def test_setup_config(self):
def test_pyproject_toml(self):
# An empty pyproject.toml file should not be used
try:
import toml
import tomli
except ImportError:
return

Expand Down
4 changes: 2 additions & 2 deletions yapftests/style_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def testErrorUnknownStyleOption(self):

def testPyprojectTomlNoYapfSection(self):
try:
import toml
import tomli
except ImportError:
return

Expand All @@ -242,7 +242,7 @@ def testPyprojectTomlNoYapfSection(self):

def testPyprojectTomlParseYapfSection(self):
try:
import toml
import tomli
except ImportError:
return

Expand Down