Thanks to visit codestin.com
Credit goes to github.com

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
9 changes: 5 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,12 @@ If you use ``pyproject.toml``, exclude patterns are specified by ``ignore_patten
in ``[tool.yapfignore]`` section. For example:

.. code-block:: ini

[tool.yapfignore]
ignore_patterns="""
temp/**/*.py
temp2/*.py
"""
ignore_patterns = [
"temp/**/*.py",
"temp2/*.py"
]

Formatting style
================
Expand Down
18 changes: 6 additions & 12 deletions yapf/yapflib/file_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,13 @@ def _GetExcludePatternsFromPyprojectToml(filename):
"toml package is needed for using pyproject.toml as a configuration file"
)

pyproject_toml = toml.load(filename)
if os.path.isfile(filename) and os.access(filename, os.R_OK):
excludes = pyproject_toml.get('tool',
{}).get('yapfignore',
{}).get('ignore_patterns', None)
if excludes is None:
return []

for line in excludes.split('\n'):
if line.strip() and not line.startswith('#'):
ignore_patterns.append(line.strip())
if any(e.startswith('./') for e in ignore_patterns):
raise errors.YapfError('path in pyproject.toml should not start with ./')
pyproject_toml = toml.load(filename)
ignore_patterns = pyproject_toml.get('tool',
{}).get('yapfignore',
{}).get('ignore_patterns', [])
if any(e.startswith('./') for e in ignore_patterns):
raise errors.YapfError('path in pyproject.toml should not start with ./')

return ignore_patterns

Expand Down
12 changes: 6 additions & 6 deletions yapftests/file_resources_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ def test_get_exclude_file_patterns_from_pyproject(self):
ignore_patterns = ['temp/**/*.py', 'temp2/*.py']
with open(local_ignore_file, 'w') as f:
f.write('[tool.yapfignore]\n')
f.write('ignore_patterns="""')
f.writelines('\n'.join(ignore_patterns))
f.write('"""')
f.write('ignore_patterns=[')
f.writelines('\n,'.join([f'"{p}"' for p in ignore_patterns]))
f.write(']')

self.assertEqual(
sorted(file_resources.GetExcludePatternsForDir(self.test_tmpdir)),
Expand All @@ -99,9 +99,9 @@ def test_get_exclude_file_patterns_from_pyproject_with_wrong_syntax(self):
ignore_patterns = ['temp/**/*.py', './wrong/syntax/*.py']
with open(local_ignore_file, 'w') as f:
f.write('[tool.yapfignore]\n')
f.write('ignore_patterns="""')
f.writelines('\n'.join(ignore_patterns))
f.write('"""')
f.write('ignore_patterns=[')
f.writelines('\n,'.join([f'"{p}"' for p in ignore_patterns]))
f.write(']')

with self.assertRaises(errors.YapfError):
file_resources.GetExcludePatternsForDir(self.test_tmpdir)
Expand Down