Skip to content

Commit

Permalink
style: enable ruff rules RUF021 and RUF022
Browse files Browse the repository at this point in the history
  • Loading branch information
d-biehl committed Dec 5, 2024
1 parent 9185ccd commit e2fbc16
Show file tree
Hide file tree
Showing 25 changed files with 109 additions and 120 deletions.
6 changes: 2 additions & 4 deletions packages/core/src/robotcode/core/concurrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,8 @@ def decorator(func: _F) -> _F:


def is_threaded_callable(callable: Callable[..., Any]) -> bool:
return (
getattr(callable, __THREADED_MARKER, False)
or inspect.ismethod(callable)
and getattr(callable, __THREADED_MARKER, False)
return getattr(callable, __THREADED_MARKER, False) or (
inspect.ismethod(callable) and getattr(callable, __THREADED_MARKER, False)
)


Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/robotcode/core/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from typing_extensions import ParamSpec

__all__ = ["event_iterator", "event"]
__all__ = ["event", "event_iterator"]

_TResult = TypeVar("_TResult")
_TParams = ParamSpec("_TParams")
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/robotcode/core/utils/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
)

__all__ = [
"to_snake_case",
"to_camel_case",
"CamelSnakeMixin",
"ValidateMixin",
"as_dict",
"as_json",
"from_dict",
"from_json",
"as_dict",
"ValidateMixin",
"CamelSnakeMixin",
"to_camel_case",
"to_snake_case",
]

_RE_SNAKE_CASE_1 = re.compile(r"[\-\.\s]")
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/robotcode/core/utils/glob_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def _iter_files_recursive_re(
relative_path = (path / f.name).relative_to(_base_path)

if not ignore_patterns or not any(
p.matches(relative_path) and (not p.only_dirs or p.only_dirs and f.is_dir())
p.matches(relative_path) and (not p.only_dirs or (p.only_dirs and f.is_dir()))
for p in cast(Iterable[Pattern], ignore_patterns)
):
if f.is_dir():
Expand All @@ -177,7 +177,7 @@ def _iter_files_recursive_re(
_base_path=_base_path,
)
if not patterns or any(
p.matches(relative_path) and (not p.only_dirs or p.only_dirs and f.is_dir())
p.matches(relative_path) and (not p.only_dirs or (p.only_dirs and f.is_dir()))
for p in cast(Iterable[Pattern], patterns)
):
yield Path(f).absolute() if absolute else Path(f)
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/robotcode/core/utils/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def log(
extra: Optional[Mapping[str, object]] = None,
**kwargs: Any,
) -> None:
if self.is_enabled_for(level) and condition is not None and condition() or condition is None:
if (self.is_enabled_for(level) and condition is not None and condition()) or condition is None:
depth = 0
if context_name is not None:
depth = self._measure_contexts.get(context_name, 0)
Expand Down
29 changes: 13 additions & 16 deletions packages/debugger/src/robotcode/debugger/debugger.py
Original file line number Diff line number Diff line change
Expand Up @@ -1318,11 +1318,8 @@ def message(self, message: Dict[str, Any]) -> None:
level = message["level"]
current_frame = self.full_stack_frames[0] if self.full_stack_frames else None

if (
self.output_messages
or current_frame is not None
and current_frame.type != "KEYWORD"
and level in ["FAIL", "ERROR", "WARN"]
if self.output_messages or (
current_frame is not None and current_frame.type != "KEYWORD" and level in ["FAIL", "ERROR", "WARN"]
):
self._send_log_event(message["timestamp"], level, message["message"], "messages")

Expand Down Expand Up @@ -1599,10 +1596,8 @@ def evaluate(
else evaluate_context.variables._global
)
if (
isinstance(context, EvaluateArgumentContext)
and context != EvaluateArgumentContext.REPL
or self.expression_mode
):
isinstance(context, EvaluateArgumentContext) and context != EvaluateArgumentContext.REPL
) or self.expression_mode:
if expression.startswith("! "):
splitted = self.SPLIT_LINE.split(expression[2:].strip())

Expand Down Expand Up @@ -1635,13 +1630,15 @@ def run_kw() -> Any:
result = vars.replace_scalar(expression)
except VariableError:
if context is not None and (
isinstance(context, EvaluateArgumentContext)
and (
context
in [
EvaluateArgumentContext.HOVER,
EvaluateArgumentContext.WATCH,
]
(
isinstance(context, EvaluateArgumentContext)
and (
context
in [
EvaluateArgumentContext.HOVER,
EvaluateArgumentContext.WATCH,
]
)
)
or context
in [
Expand Down
25 changes: 12 additions & 13 deletions packages/jsonrpc2/src/robotcode/jsonrpc2/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,24 @@
from robotcode.core.utils.logging import LoggingDescriptor

__all__ = [
"JsonRPCErrors",
"JsonRPCMessage",
"JsonRPCNotification",
"JsonRPCRequest",
"JsonRPCResponse",
"GenericJsonRPCProtocolPart",
"InvalidProtocolVersionError",
"JsonRPCError",
"JsonRPCErrorException",
"JsonRPCErrorObject",
"JsonRPCProtocol",
"JsonRPCErrors",
"JsonRPCException",
"JsonRPCMessage",
"JsonRPCNotification",
"JsonRPCParseError",
"InvalidProtocolVersionError",
"rpc_method",
"RpcRegistry",
"JsonRPCProtocol",
"JsonRPCProtocolPart",
"JsonRPCRequest",
"JsonRPCResponse",
"ProtocolPartDescriptor",
"GenericJsonRPCProtocolPart",
"RpcRegistry",
"TProtocol",
"JsonRPCErrorException",
"rpc_method",
]

_T = TypeVar("_T")
Expand Down Expand Up @@ -278,8 +278,7 @@ def get_methods(obj: Any) -> Dict[str, RpcMethodEntry]:
iter_methods(
obj,
lambda m2: isinstance(m2, RpcMethod)
or inspect.ismethod(m2)
and isinstance(m2.__func__, RpcMethod),
or (inspect.ismethod(m2) and isinstance(m2.__func__, RpcMethod)),
),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
from robotcode.language_server.common.protocol import LanguageServerProtocol


__all__ = ["TextDocumentProtocolPart", "LanguageServerDocumentError"]
__all__ = ["LanguageServerDocumentError", "TextDocumentProtocolPart"]


class LanguageServerDocumentError(JsonRPCException):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from .protocol import LanguageServerProtocol

__all__ = ["LanguageServerBase", "TCP_DEFAULT_PORT"]
__all__ = ["TCP_DEFAULT_PORT", "LanguageServerBase"]

TCP_DEFAULT_PORT = 6610

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,23 +194,18 @@ def get_valid_nodes_in_range(self, model: ast.AST, range: Range, also_return: bo

r = range_from_node(node, skip_non_data=True, allow_comments=True)
if r.is_in_range(range):
if (
isinstance(
node,
(
Fixture,
Documentation,
MultiValue,
SingleValue,
TestCaseName,
KeywordName,
TemplateArguments,
),
)
or also_return
and get_robot_version() >= (5, 0, 0)
and isinstance(node, ReturnStatement)
):
if isinstance(
node,
(
Fixture,
Documentation,
MultiValue,
SingleValue,
TestCaseName,
KeywordName,
TemplateArguments,
),
) or (also_return and get_robot_version() >= (5, 0, 0) and isinstance(node, ReturnStatement)):
return []

result.append(node)
Expand Down Expand Up @@ -246,19 +241,16 @@ def get_valid_nodes_in_range(self, model: ast.AST, range: Range, also_return: bo
n
for n in result
if isinstance(n, (IfHeader, ElseIfHeader, ElseHeader, ForHeader, End))
or get_robot_version() >= (5, 0)
and isinstance(n, (WhileHeader, TryHeader, ExceptHeader, FinallyHeader))
or (get_robot_version() >= (5, 0) and isinstance(n, (WhileHeader, TryHeader, ExceptHeader, FinallyHeader)))
):
return []

if get_robot_version() >= (5, 0, 0) and any(
n
for n in result
if isinstance(n, (Continue, Break))
or isinstance(n, Try)
and n.type in [RobotToken.EXCEPT, RobotToken.FINALLY, RobotToken.ELSE]
or also_return
and isinstance(n, ReturnStatement)
or (isinstance(n, Try) and n.type in [RobotToken.EXCEPT, RobotToken.FINALLY, RobotToken.ELSE])
or (also_return and isinstance(n, ReturnStatement))
):
return []

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def _find_method(self, cls: Type[Any]) -> Optional[_HandlerMethod]:
@_logger.call
def collect(self, sender: Any, document: TextDocument, range: Range) -> Optional[List[InlayHint]]:
config = self.get_config(document)
if config is None or not config.parameter_names and not config.namespaces:
if config is None or (not config.parameter_names and not config.namespaces):
return None

model = self.parent.documents_cache.get_model(document, False)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,7 @@ def generate_sem_sub_tokens(
if (
yield_arguments
or token.type != Token.ARGUMENT
or token.type != Token.NAME
and cached_isinstance(node, Metadata)
or (token.type != Token.NAME and cached_isinstance(node, Metadata))
):
yield SemTokenInfo.from_token(token, sem_type, sem_mod, col_offset, length)

Expand All @@ -632,10 +631,8 @@ def generate_sem_tokens(
namespace: Namespace,
builtin_library_doc: Optional[LibraryDoc],
) -> Iterator[SemTokenInfo]:
if (
token.type in {Token.ARGUMENT, Token.TESTCASE_NAME, Token.KEYWORD_NAME}
or token.type == Token.NAME
and cached_isinstance(node, VariablesImport, LibraryImport, ResourceImport)
if token.type in {Token.ARGUMENT, Token.TESTCASE_NAME, Token.KEYWORD_NAME} or (
token.type == Token.NAME and cached_isinstance(node, VariablesImport, LibraryImport, ResourceImport)
):
if (
cached_isinstance(node, Variable) and token.type == Token.ARGUMENT and node.name and node.name[0] == "&"
Expand Down
10 changes: 5 additions & 5 deletions packages/plugin/src/robotcode/plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
from robotcode.core.utils.dataclasses import as_dict, as_json

__all__ = [
"hookimpl",
"CommonConfig",
"pass_application",
"Application",
"UnknownError",
"OutputFormat",
"ColoredOutput",
"CommonConfig",
"OutputFormat",
"UnknownError",
"hookimpl",
"pass_application",
]

F = TypeVar("F", bound=Callable[..., Any])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def modify_diagnostic(self, diagnostic: Diagnostic) -> Optional[Diagnostic]:

lines = self.rules_and_codes.codes.get(code)

if lines is None or lines is not None and diagnostic.range.start.line not in lines:
if lines is None or (lines is not None and diagnostic.range.start.line not in lines):
code = "*"
lines = self.rules_and_codes.codes.get(code)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,7 @@ def check_file_changed(self, changes: List[FileEvent]) -> Optional[FileChangeTyp
if (
self._document is not None
and (normalized_path(path) == normalized_path(self._document.uri.to_path()))
or self._document is None
):
) or self._document is None:
self._invalidate()

return change.type
Expand Down
Loading

0 comments on commit e2fbc16

Please sign in to comment.