Skip to content

Commit

Permalink
Apply some pyupgrade suggestions
Browse files Browse the repository at this point in the history
io.open() is an alias for for the builtin open() function:
	https://docs.python.org/3/library/io.html#io.open

If open() cannot open a file, an OSError is raised:
	https://docs.python.org/3/library/functions.html#open

EnvironmentError is kept for compatibility with previous versions;
starting from Python 3.3, it is an alias of OSError:
	https://docs.python.org/3/library/exceptions.html#EnvironmentError

Directly yield from an iterable instead of iterating to yield items.
  • Loading branch information
DimitriPapadopoulos authored and adrienverge committed Oct 28, 2022
1 parent 5b21a3d commit 5ac3ed4
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 11 deletions.
1 change: 0 additions & 1 deletion tests/test_spec_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from io import open
import os

from tests.common import RuleTestCase
Expand Down
7 changes: 3 additions & 4 deletions yamllint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import argparse
import io
import locale
import os
import platform
Expand Down Expand Up @@ -213,9 +212,9 @@ def run(argv=None):
for file in find_files_recursively(args.files, conf):
filepath = file[2:] if file.startswith('./') else file
try:
with io.open(file, newline='') as f:
with open(file, newline='') as f:
problems = linter.run(f, conf, filepath)
except EnvironmentError as e:
except OSError as e:
print(e, file=sys.stderr)
sys.exit(-1)
prob_level = show_problems(problems, file, args_format=args.format,
Expand All @@ -226,7 +225,7 @@ def run(argv=None):
if args.stdin:
try:
problems = linter.run(sys.stdin, conf, '')
except EnvironmentError as e:
except OSError as e:
print(e, file=sys.stderr)
sys.exit(-1)
prob_level = show_problems(problems, 'stdin', args_format=args.format,
Expand Down
3 changes: 1 addition & 2 deletions yamllint/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ def token_or_comment_generator(buffer):

yield Token(curr.start_mark.line + 1, curr, prev, next, nextnext)

for comment in comments_between_tokens(curr, next):
yield comment
yield from comments_between_tokens(curr, next)

prev = curr
curr = next
Expand Down
6 changes: 2 additions & 4 deletions yamllint/rules/indentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,7 @@ def detect_indent(base_indent, next):

if (isinstance(token, yaml.ScalarToken) and
conf['check-multi-line-strings']):
for problem in check_scalar_indentation(conf, token, context):
yield problem
yield from check_scalar_indentation(conf, token, context)

# Step 2.a:

Expand Down Expand Up @@ -581,8 +580,7 @@ def detect_indent(base_indent, next):

def check(conf, token, prev, next, nextnext, context):
try:
for problem in _check(conf, token, prev, next, nextnext, context):
yield problem
yield from _check(conf, token, prev, next, nextnext, context)
except AssertionError:
yield LintProblem(token.start_mark.line + 1,
token.start_mark.column + 1,
Expand Down

0 comments on commit 5ac3ed4

Please sign in to comment.