Skip to content

Commit

Permalink
Review
Browse files Browse the repository at this point in the history
  • Loading branch information
konstin committed Sep 26, 2023
1 parent 549c698 commit b64b4a1
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ def opener(path, flags):

open(p, closefd=False)
open(p, opener=opener)
open(p, mode='r', buffering=- 1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
open(p, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
open(p, 'r', - 1, None, None, None, True, None)
open(p, 'r', - 1, None, None, None, False, opener)
16 changes: 8 additions & 8 deletions crates/ruff_linter/resources/test/fixtures/pyupgrade/UP015.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@
with open(mode="Ub", name="foo") as f:
pass

open(file="foo", mode='U', buffering=- 1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
open(file="foo", buffering=- 1, encoding=None, errors=None, newline=None, closefd=True, opener=None, mode='U')
open(file="foo", buffering=- 1, encoding=None, errors=None, mode='U', newline=None, closefd=True, opener=None)
open(mode='U', file="foo", buffering=- 1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
open(file="foo", mode='U', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
open(file="foo", buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None, mode='U')
open(file="foo", buffering=-1, encoding=None, errors=None, mode='U', newline=None, closefd=True, opener=None)
open(mode='U', file="foo", buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

open(file="foo", mode='Ub', buffering=- 1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
open(file="foo", buffering=- 1, encoding=None, errors=None, newline=None, closefd=True, opener=None, mode='Ub')
open(file="foo", buffering=- 1, encoding=None, errors=None, mode='Ub', newline=None, closefd=True, opener=None)
open(mode='Ub', file="foo", buffering=- 1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
open(file="foo", mode='Ub', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
open(file="foo", buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None, mode='Ub')
open(file="foo", buffering=-1, encoding=None, errors=None, mode='Ub', newline=None, closefd=True, opener=None)
open(mode='Ub', file="foo", buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

open = 1
open("foo", "U")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ruff_diagnostics::{Diagnostic, DiagnosticKind};
use ruff_python_ast::{Constant, ExprCall};
use ruff_python_ast::{Constant, Expr, ExprCall, ExprConstant};
use ruff_text_size::Ranged;

use crate::checkers::ast::Checker;
Expand Down Expand Up @@ -91,28 +91,40 @@ pub(crate) fn replaceable_by_pathlib(checker: &mut Checker, call: &ExprCall) {
// are set to non-default values.
// https://github.com/astral-sh/ruff/issues/7620
// Signature as of Python 3.11 (https://docs.python.org/3/library/functions.html#open):
// ```
// ```text
// 0 1 2 3 4 5
// open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None,
// 6 7
// closefd=True, opener=None)
// ^^^^ ^^^^
// ```
// For `pathlib` (https://docs.python.org/3/library/pathlib.html#pathlib.Path.open):
// ```text
// Path.open(mode='r', buffering=-1, encoding=None, errors=None, newline=None)
// ```
if call
.arguments
.find_argument("closefd", 6)
.is_some_and(|expr| {
!expr.as_constant_expr().is_some_and(|constant| {
matches!(constant.value, Constant::Bool(true))
})
!matches!(
expr,
Expr::Constant(ExprConstant {
value: Constant::Bool(true),
..
})
)
})
|| call
.arguments
.find_argument("opener", 7)
.is_some_and(|expr| {
!expr.as_constant_expr().is_some_and(|constant| {
matches!(constant.value, Constant::None)
})
!matches!(
expr,
Expr::Constant(ExprConstant {
value: Constant::None,
..
})
)
})
{
return None;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ full_name.py:44:1: PTH123 `open()` should be replaced by `Path.open()`
|
42 | open(p, closefd=False)
43 | open(p, opener=opener)
44 | open(p, mode='r', buffering=- 1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
44 | open(p, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
| ^^^^ PTH123
45 | open(p, 'r', - 1, None, None, None, True, None)
46 | open(p, 'r', - 1, None, None, None, False, opener)
Expand All @@ -292,7 +292,7 @@ full_name.py:44:1: PTH123 `open()` should be replaced by `Path.open()`
full_name.py:45:1: PTH123 `open()` should be replaced by `Path.open()`
|
43 | open(p, opener=opener)
44 | open(p, mode='r', buffering=- 1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
44 | open(p, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
45 | open(p, 'r', - 1, None, None, None, True, None)
| ^^^^ PTH123
46 | open(p, 'r', - 1, None, None, None, False, opener)
Expand Down
Loading

0 comments on commit b64b4a1

Please sign in to comment.