Skip to content

Commit

Permalink
Merge branch 'ershi/ruff-pylint' into 'main'
Browse files Browse the repository at this point in the history
Enable pylint rules and fix associated issues

See merge request omniverse/warp!888
  • Loading branch information
mmacklin committed Dec 9, 2024
2 parents 4940d22 + 7ae01ff commit 8fa9e35
Show file tree
Hide file tree
Showing 17 changed files with 111 additions and 246 deletions.
2 changes: 1 addition & 1 deletion build_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
export_stubs(stub_file)

# code formatting of stubs.py
subprocess.run(["ruff", "format", "--verbose", os.path.join(base_path, "warp", "stubs.py")])
subprocess.run(["ruff", "format", "--verbose", os.path.join(base_path, "warp", "stubs.py")], check=True)

with open(os.path.join(base_path, "docs", "modules", "functions.rst"), "w") as function_ref:
export_functions_rst(function_ref)
Expand Down
2 changes: 1 addition & 1 deletion exts/omni.warp.core/omni/warp/core/_impl/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def on_startup(self, ext_id):
# Junctions don't support relative paths so we need
# to use an absolute path for the destination.
cmd = ("mklink", "/j", LOCAL_LIB_PATH, LIB_PATH_ABS)
subprocess.run(cmd, stdout=subprocess.DEVNULL, shell=True)
subprocess.run(cmd, stdout=subprocess.DEVNULL, shell=True, check=True)
else:
raise RuntimeError(f"Failed to create the symlink `{LOCAL_LIB_PATH}`") from e

Expand Down
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ select = [
"B", # flake8-bugbear
"C4", # flake8-comprehensions
"NPY", # NumPy-specific rules
"PLC", # pylint convention
"PLE", # pylint error
"PLW" # pylint warning
]
ignore = [
"E501", # Many lines are over 120 characters already
Expand All @@ -67,6 +70,7 @@ ignore = [
"F405", # Related to use of wildcard imports
"F811", # Warp often uses overloads
"E721", # Warp often uses == in float and int type comparisons
"PLW0603" # Allow assignments to global variables
]

[tool.ruff.lint.per-file-ignores]
Expand Down
10 changes: 5 additions & 5 deletions warp/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,11 +399,11 @@ def scalar_infer_type(arg_types: Mapping[str, type]):

scalar_types = set()
for t in arg_types:
t = strip_reference(t)
if hasattr(t, "_wp_scalar_type_"):
scalar_types.add(t._wp_scalar_type_)
elif t in scalar_and_bool_types:
scalar_types.add(t)
t_val = strip_reference(t)
if hasattr(t_val, "_wp_scalar_type_"):
scalar_types.add(t_val._wp_scalar_type_)
elif t_val in scalar_and_bool_types:
scalar_types.add(t_val)

if len(scalar_types) > 1:
raise RuntimeError(
Expand Down
38 changes: 21 additions & 17 deletions warp/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1175,25 +1175,25 @@ def add_comp(adj, op_strings, left, comps):
left = adj.load(left)
s = output.emit() + " = " + ("(" * len(comps)) + left.emit() + " "

prev_comp = None
prev_comp_var = None

for op, comp in zip(op_strings, comps):
comp_chainable = op_str_is_chainable(op)
if comp_chainable and prev_comp:
# We restrict chaining to operands of the same type
if prev_comp.type is comp.type:
prev_comp = adj.load(prev_comp)
comp = adj.load(comp)
s += "&& (" + prev_comp.emit() + " " + op + " " + comp.emit() + ")) "
if comp_chainable and prev_comp_var:
# We restrict chaining to operands of the same type
if prev_comp_var.type is comp.type:
prev_comp_var = adj.load(prev_comp_var)
comp_var = adj.load(comp)
s += "&& (" + prev_comp_var.emit() + " " + op + " " + comp_var.emit() + ")) "
else:
raise WarpCodegenTypeError(
f"Cannot chain comparisons of unequal types: {prev_comp.type} {op} {comp.type}."
f"Cannot chain comparisons of unequal types: {prev_comp_var.type} {op} {comp.type}."
)
else:
comp = adj.load(comp)
s += op + " " + comp.emit() + ") "
comp_var = adj.load(comp)
s += op + " " + comp_var.emit() + ") "

prev_comp = comp
prev_comp_var = comp_var

s = s.rstrip() + ";"

Expand Down Expand Up @@ -1366,13 +1366,15 @@ def add_call(adj, func, args, kwargs, type_args, min_outputs=None):
fwd_args = []
for func_arg in func_args:
if not isinstance(func_arg, (Reference, warp.context.Function)):
func_arg = adj.load(func_arg)
func_arg_var = adj.load(func_arg)
else:
func_arg_var = func_arg

# if the argument is a function (and not a builtin), then build it recursively
if isinstance(func_arg, warp.context.Function) and not func_arg.is_builtin():
adj.builder.build_function(func_arg)
if isinstance(func_arg_var, warp.context.Function) and not func_arg_var.is_builtin():
adj.builder.build_function(func_arg_var)

fwd_args.append(strip_reference(func_arg))
fwd_args.append(strip_reference(func_arg_var))

if return_type is None:
# handles expression (zero output) functions, e.g.: void do_something();
Expand Down Expand Up @@ -2580,8 +2582,10 @@ def emit_Return(adj, node):
adj.return_var = ()
for ret in var:
if is_reference(ret.type):
ret = adj.add_builtin_call("copy", [ret])
adj.return_var += (ret,)
ret_var = adj.add_builtin_call("copy", [ret])
else:
ret_var = ret
adj.return_var += (ret_var,)

adj.add_return(adj.return_var)

Expand Down
38 changes: 18 additions & 20 deletions warp/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,24 +238,23 @@ def __init__(
# in a way that is compatible with Python's semantics.
signature_params = []
signature_default_param_kind = inspect.Parameter.POSITIONAL_OR_KEYWORD
for param_name in self.input_types.keys():
if param_name.startswith("**"):
param_name = param_name[2:]
for raw_param_name in self.input_types.keys():
if raw_param_name.startswith("**"):
param_name = raw_param_name[2:]
param_kind = inspect.Parameter.VAR_KEYWORD
elif param_name.startswith("*"):
param_name = param_name[1:]
elif raw_param_name.startswith("*"):
param_name = raw_param_name[1:]
param_kind = inspect.Parameter.VAR_POSITIONAL

# Once a variadic argument like `*args` is found, any following
# arguments need to be passed using keywords.
signature_default_param_kind = inspect.Parameter.KEYWORD_ONLY
else:
param_name = raw_param_name
param_kind = signature_default_param_kind

param = param = inspect.Parameter(
param_name,
param_kind,
default=self.defaults.get(param_name, inspect.Parameter.empty),
param = inspect.Parameter(
param_name, param_kind, default=self.defaults.get(param_name, inspect.Parameter.empty)
)
signature_params.append(param)
self.signature = inspect.Signature(signature_params)
Expand Down Expand Up @@ -509,11 +508,10 @@ def call_builtin(func: Function, *params) -> Tuple[bool, Any]:
if elem_count != arg_type._length_:
return (False, None)

# Retrieve the element type of the sequence while ensuring
# that it's homogeneous.
# Retrieve the element type of the sequence while ensuring that it's homogeneous.
elem_type = type(arr[0])
for i in range(1, elem_count):
if type(arr[i]) is not elem_type:
for array_index in range(1, elem_count):
if type(arr[array_index]) is not elem_type:
raise ValueError("All array elements must share the same type.")

expected_elem_type = arg_type._wp_scalar_type_
Expand Down Expand Up @@ -543,10 +541,10 @@ def call_builtin(func: Function, *params) -> Tuple[bool, Any]:
c_param = arg_type()
if warp.types.type_is_matrix(arg_type):
rows, cols = arg_type._shape_
for i in range(rows):
idx_start = i * cols
for row_index in range(rows):
idx_start = row_index * cols
idx_end = idx_start + cols
c_param[i] = arr[idx_start:idx_end]
c_param[row_index] = arr[idx_start:idx_end]
else:
c_param[:] = arr

Expand Down Expand Up @@ -1239,16 +1237,16 @@ def initializer_list_func(args, return_type):
typelists.append(l)

for arg_types in itertools.product(*typelists):
arg_types = dict(zip(input_types.keys(), arg_types))
concrete_arg_types = dict(zip(input_types.keys(), arg_types))

# Some of these argument lists won't work, eg if the function is mul(), we won't be
# able to do a matrix vector multiplication for a mat22 and a vec3. The `constraint`
# function determines which combinations are valid:
if constraint:
if constraint(arg_types) is False:
if constraint(concrete_arg_types) is False:
continue

return_type = value_func(arg_types, None)
return_type = value_func(concrete_arg_types, None)

# The return_type might just be vector_t(length=3,dtype=wp.float32), so we've got to match that
# in the list of hard coded types so it knows it's returning one of them:
Expand All @@ -1266,7 +1264,7 @@ def initializer_list_func(args, return_type):
# finally we can generate a function call for these concrete types:
add_builtin(
key,
input_types=arg_types,
input_types=concrete_arg_types,
value_type=return_type,
value_func=value_func if return_type is Any else None,
export_func=export_func,
Expand Down
2 changes: 0 additions & 2 deletions warp/fem/geometry/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ def __str__(self) -> str:
SideIndexArg: wp.codegen.Struct
"""Structure containing arguments to be passed to device functions for indexing sides"""

@staticmethod
def cell_arg_value(self, device) -> "Geometry.CellArg":
"""Value of the arguments to be passed to cell-related device functions"""
raise NotImplementedError
Expand Down Expand Up @@ -107,7 +106,6 @@ def cell_normal(args: "Geometry.CellArg", s: "Sample"):
For elements with the same dimension as the embedding space, this will be zero."""
raise NotImplementedError

@staticmethod
def side_arg_value(self, device) -> "Geometry.SideArg":
"""Value of the arguments to be passed to side-related device functions"""
raise NotImplementedError
Expand Down
16 changes: 8 additions & 8 deletions warp/sim/import_urdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,14 @@ def parse_shapes(link, geoms, density, incoming_xform=None, visible=True, just_v
if hasattr(m, "geometry"):
# multiple meshes are contained in a scene
for geom in m.geometry.values():
vertices = np.array(geom.vertices, dtype=np.float32) * scaling
faces = np.array(geom.faces.flatten(), dtype=np.int32)
mesh = Mesh(vertices, faces)
geom_vertices = np.array(geom.vertices, dtype=np.float32) * scaling
geom_faces = np.array(geom.faces.flatten(), dtype=np.int32)
geom_mesh = Mesh(geom_vertices, geom_faces)
s = builder.add_shape_mesh(
body=link,
pos=wp.vec3(tf.p),
rot=wp.quat(tf.q),
mesh=mesh,
mesh=geom_mesh,
density=density,
is_visible=visible,
has_ground_collision=not just_visual,
Expand All @@ -228,14 +228,14 @@ def parse_shapes(link, geoms, density, incoming_xform=None, visible=True, just_v
shapes.append(s)
else:
# a single mesh
vertices = np.array(m.vertices, dtype=np.float32) * scaling
faces = np.array(m.faces.flatten(), dtype=np.int32)
mesh = Mesh(vertices, faces)
m_vertices = np.array(m.vertices, dtype=np.float32) * scaling
m_faces = np.array(m.faces.flatten(), dtype=np.int32)
m_mesh = Mesh(m_vertices, m_faces)
s = builder.add_shape_mesh(
body=link,
pos=wp.vec3(tf.p),
rot=wp.quat(tf.q),
mesh=mesh,
mesh=m_mesh,
density=density,
is_visible=visible,
has_ground_collision=not just_visual,
Expand Down
24 changes: 13 additions & 11 deletions warp/sim/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1442,12 +1442,14 @@ def add_builder(self, builder, xform=None, update_num_env_count=True, separate_c
self.shape_collision_filter_pairs.add((i + shape_count, j + shape_count))
for group, shapes in builder.shape_collision_group_map.items():
if separate_collision_group:
group = self.last_collision_group + 1
extend_group = self.last_collision_group + 1
else:
group = group + self.last_collision_group if group > -1 else -1
if group not in self.shape_collision_group_map:
self.shape_collision_group_map[group] = []
self.shape_collision_group_map[group].extend([s + shape_count for s in shapes])
extend_group = group + self.last_collision_group if group > -1 else -1

if extend_group not in self.shape_collision_group_map:
self.shape_collision_group_map[extend_group] = []

self.shape_collision_group_map[extend_group].extend([s + shape_count for s in shapes])

# update last collision group counter
if separate_collision_group:
Expand Down Expand Up @@ -2616,11 +2618,12 @@ def dfs(parent_body: int, child_body: int, incoming_xform: wp.transform, last_dy
joint_remap[joint["original_id"]] = i
# update articulation_start
for i, old_i in enumerate(self.articulation_start):
while old_i not in joint_remap:
old_i += 1
if old_i >= self.joint_count:
start_i = old_i
while start_i not in joint_remap:
start_i += 1
if start_i >= self.joint_count:
break
self.articulation_start[i] = joint_remap.get(old_i, old_i)
self.articulation_start[i] = joint_remap.get(start_i, start_i)
# remove empty articulation starts, i.e. where the start and end are the same
self.articulation_start = list(set(self.articulation_start))

Expand Down Expand Up @@ -4269,8 +4272,7 @@ def add_face(i, j, k):
pos = wp.vec3(pos[0], pos[1], pos[2])
# add particles
for v in vertices:
v = wp.vec3(v[0], v[1], v[2])
p = wp.quat_rotate(rot, v * scale) + pos
p = wp.quat_rotate(rot, wp.vec3(v[0], v[1], v[2]) * scale) + pos

self.add_particle(p, vel, 0.0)

Expand Down
2 changes: 1 addition & 1 deletion warp/sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

# typing hints

_BlockType = TypeVar("BlockType")
_BlockType = TypeVar("BlockType") # noqa: PLC0132


class _MatrixBlockType(Generic[Rows, Cols, Scalar]):
Expand Down
4 changes: 3 additions & 1 deletion warp/tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ def run(test, device):

# with wp.ScopedTimer(f"{name}_{sanitize_identifier(device)}"):
# Run the script as a subprocess
result = subprocess.run(command, capture_output=True, text=True, env=env_vars, timeout=test_timeout)
result = subprocess.run(
command, capture_output=True, text=True, env=env_vars, timeout=test_timeout, check=False
)

# Check the return code (0 is standard for success)
test.assertEqual(
Expand Down
Loading

0 comments on commit 8fa9e35

Please sign in to comment.