Skip to content

Commit 5fbe0f6

Browse files
committed
Remove usage of isinstance() with unioned types
Ruff's UP038 that enforced this is deprecated because it's slower.
1 parent d7f8fd2 commit 5fbe0f6

File tree

10 files changed

+14
-13
lines changed

10 files changed

+14
-13
lines changed

django_mongodb_backend/compiler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ def get_lookup_pipeline(self):
669669
if (
670670
isinstance(expr, Lookup)
671671
and isinstance(expr.lhs, Col)
672-
and (is_direct_value(expr.rhs) or isinstance(expr.rhs, Value | Col))
672+
and (is_direct_value(expr.rhs) or isinstance(expr.rhs, (Value, Col)))
673673
):
674674
pushed_filters[expr.lhs.alias].append(expr)
675675
for alias in tuple(self.query.alias_map):

django_mongodb_backend/expressions/builtins.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def when(self, compiler, connection):
187187

188188
def value(self, compiler, connection): # noqa: ARG001
189189
value = self.value
190-
if isinstance(value, list | int):
190+
if isinstance(value, (list, int)):
191191
# Wrap lists & numbers in $literal to prevent ambiguity when Value
192192
# appears in $project.
193193
return {"$literal": value}

django_mongodb_backend/expressions/search.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ def __repr__(self):
4242

4343
class SearchCombinable:
4444
def _combine(self, other, connector):
45-
if not isinstance(self, CompoundExpression | CombinedSearchExpression):
45+
if not isinstance(self, (CompoundExpression, CombinedSearchExpression)):
4646
lhs = CompoundExpression(must=[self])
4747
else:
4848
lhs = self
49-
if other and not isinstance(other, CompoundExpression | CombinedSearchExpression):
49+
if other and not isinstance(other, (CompoundExpression, CombinedSearchExpression)):
5050
rhs = CompoundExpression(must=[other])
5151
else:
5252
rhs = other

django_mongodb_backend/fields/array.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def model(self, model):
6363

6464
@classmethod
6565
def _choices_is_value(cls, value):
66-
return isinstance(value, list | tuple) or super()._choices_is_value(value)
66+
return isinstance(value, (list, tuple)) or super()._choices_is_value(value)
6767

6868
def check(self, **kwargs):
6969
errors = super().check(**kwargs)
@@ -126,7 +126,7 @@ def db_type(self, connection):
126126
return "array"
127127

128128
def get_db_prep_value(self, value, connection, prepared=False):
129-
if isinstance(value, list | tuple):
129+
if isinstance(value, (list, tuple)):
130130
return [self.base_field.get_db_prep_value(i, connection, prepared=False) for i in value]
131131
return value
132132

@@ -236,7 +236,7 @@ def as_mql(self, compiler, connection):
236236

237237
class ArrayRHSMixin:
238238
def __init__(self, lhs, rhs):
239-
if isinstance(rhs, tuple | list):
239+
if isinstance(rhs, (tuple, list)):
240240
expressions = []
241241
for value in rhs:
242242
if not hasattr(value, "resolve_expression"):

django_mongodb_backend/fields/embedded_model_array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def deconstruct(self):
3939
return name, path, args, kwargs
4040

4141
def get_db_prep_value(self, value, connection, prepared=False):
42-
if isinstance(value, list | tuple):
42+
if isinstance(value, (list, tuple)):
4343
# Must call get_db_prep_save() rather than get_db_prep_value()
4444
# to transform model instances to dicts.
4545
return [self.base_field.get_db_prep_save(i, connection) for i in value]

django_mongodb_backend/fields/json.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def has_key_lookup(self, compiler, connection):
6565
"""Return MQL to check for the existence of a key."""
6666
rhs = self.rhs
6767
lhs = process_lhs(self, compiler, connection)
68-
if not isinstance(rhs, list | tuple):
68+
if not isinstance(rhs, (list, tuple)):
6969
rhs = [rhs]
7070
paths = []
7171
# Transform any "raw" keys into KeyTransforms to allow consistent handling

django_mongodb_backend/fields/polymorphic_embedded_model_array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def deconstruct(self):
4343
return name, path, args, kwargs
4444

4545
def get_db_prep_value(self, value, connection, prepared=False):
46-
if isinstance(value, list | tuple):
46+
if isinstance(value, (list, tuple)):
4747
# Must call get_db_prep_save() rather than get_db_prep_value()
4848
# to transform model instances to dicts.
4949
return [self.base_field.get_db_prep_save(i, connection) for i in value]

django_mongodb_backend/indexes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class VectorSearchIndex(SearchIndex):
170170
def __init__(self, *, fields=(), name=None, similarities):
171171
super().__init__(fields=fields, name=name)
172172
self.similarities = similarities
173-
self._multiple_similarities = isinstance(similarities, tuple | list)
173+
self._multiple_similarities = isinstance(similarities, (tuple, list))
174174
for func in similarities if self._multiple_similarities else (similarities,):
175175
if func not in self.VALID_SIMILARITIES:
176176
raise ValueError(
@@ -200,7 +200,7 @@ def check(self, model, connection):
200200
id=f"{self._error_id_prefix}.E002",
201201
)
202202
)
203-
if not isinstance(field.base_field, FloatField | IntegerField):
203+
if not isinstance(field.base_field, (FloatField, IntegerField)):
204204
errors.append(
205205
Error(
206206
"VectorSearchIndex requires the base field of "

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ ignore = [
9696
"S101", # Use of `assert` detected
9797
"RUF012", # Mutable class attributes should be annotated with `typing.ClassVar`
9898
"PLW2901", # `for` loop variable `value` overwritten by assignment target
99+
"UP038", # non-pep604-isinstance is deprecated
99100
]
100101
unfixable = []
101102
exclude = []

tests/atlas_search_/test_search.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def decorator(assert_func):
4848
@wraps(assert_func)
4949
def wrapper(self, fetch, *args, **kwargs):
5050
start = monotonic()
51-
if not isinstance(fetch, Callable | QuerySet):
51+
if not isinstance(fetch, (Callable, QuerySet)):
5252
raise ValueError(
5353
"The first argument to a delayed assertion must be a QuerySet or a callable "
5454
"that returns the value to be asserted."

0 commit comments

Comments
 (0)