ruff UP001
labelled unknown
Analysis Nemotron 3 Super
Flags redundant '__metaclass__ = type' assignments in class bodies.
Fix: Removes the '__metaclass__ = type' line from the class body, preserving all other statements, comments, and formatting; if the class body becomes empty, it is replaced with 'pass'.
10 hazards
- The rewrite removes a '__metaclass__ = type' statement that may be relied upon by a metaclass hook during class creation, altering the class's metaclass resolution.
- If '__metaclass__ = type' is used to suppress a warning or satisfy a static analysis tool, removing it may reintroduce that warning or failure.
- The rewrite may eliminate a '__metaclass__ = type' line that serves as a placeholder or documentation artifact, changing the intent of the code.
- If the class contains only '__metaclass__ = type' and a comment, removing the line leaves only the comment, which may be invalid if the comment is not a valid standalone statement (though Python allows comments alone, this may affect tooling expectations).
- In rare cases where '__metaclass__ = type' is dynamically assigned via exec or eval in the surrounding scope, removing it could affect name binding in the class's local namespace.
- If '__metaclass__ = type' is part of a larger pattern where its presence affects the order of decorator evaluation or class decorator application, removing it could change behavior (though decorators are evaluated before the class body, this is unlikely but noted for completeness).
- The rewrite may interfere with tools that scan for '__metaclass__ = type' as a marker for code generation or transformation, causing those tools to skip the class.
- If '__metaclass__ = type' is used to intentionally shadow a built-in or global 'type' in the class namespace (though this is nonsensical for metaclass purpose), removing it changes the namespace.
- The rewrite may remove a '__metaclass__ = type' line that is followed by a trailing comment on the same line, causing the comment to be lost if not handled properly by the autofix.
- If the class body contains '__metaclass__ = type' and the autofix incorrectly removes indentation or blank lines, it could alter the visual structure and lead to parsing errors in edge cases (e.g., mixing tabs and spaces).
Programs
| id | result | title | hazard |
|---|---|---|---|
| r1-p001 | fix ok | Indentation and Blank Line Preservation Hazard | If the autofix incorrectly removes indentation or blank lines while deleting '__metaclass__ = type', it could corrupt visual structure and cause parsing errors in edge cases involving mixed tabs/space |
| r1-p002 | fix ok | Numeric Coercion and Rounding Hazard | If '__metaclass__ = type' is removed and the class is used in numeric contexts, incorrect coercion due to altered namespace or inheritance could affect float/int/bool behavior, Decimal precision, or d |
| r1-p003 | fix ok | Static Analysis Warning Suppression Hazard | If '__metaclass__ = type' is used to satisfy a static analysis tool (e.g., to avoid 'abstract class' warnings), removing it may cause the tool to re-emit warnings or fail validation. |
| r1-p004 | fix ok | Keyword-Only and Defaults Evaluation Hazard | If the fix alters the class body structure, it could interfere with default argument evaluation in methods defined later, especially if closures capture class state affected by metaclass presence. |
| r1-p005 | fix ok prediction off | Decorator Evaluation Order Hazard | Though rare, if '__metaclass__ = type' were mistakenly believed to affect decorator order (e.g., via __classcell__ or similar), removing it could change behavior in advanced metaprogramming. |
| r1-p006 | fix ok | Intentional Built-in Shadowing Hazard | If '__metaclass__ = type' is used to shadow the built-in 'type' in the class namespace (even if nonsensical), removing it changes what 'type' refers to inside the class body, affecting any code that u |
| r1-p007 | silent prediction off | Evaluation Order and Side Effects Hazard | If '__metaclass__ = type' has side effects in its evaluation (e.g., via a property or descriptor), removing it could alter the order of execution or eliminate side effects that other code depends on. |
| r1-p008 | fix ok | Pathlib vs os.path Behavior Hazard | If the autofix breaks formatting (e.g., removes a blank line) and the class is used in a context involving path resolution (e.g., as a namespace for path constants), relative path interpretation or sy |
| r2-p001 | fix ok | Code Generation Tool Marker | The rewrite may interfere with tools that scan for '__metaclass__ = type' as a marker for code generation or transformation, causing those tools to skip the class. |
| r2-p002 | fix ok | Placeholder Documentation Artifact | The rewrite may eliminate a '__metaclass__ = type' line that serves as a placeholder or documentation artifact, changing the intent of the code. |
| r2-p003 | fix ok | String Formatting Edge Cases | string formatting edge cases: %, format, f-strings, repr vs str, nested quotes |
| r2-p004 | fix ok | Exceptions Raised Inside Rewritten Code | exceptions raised inside the rewritten code, finally/else clauses, exception chaining |
| r2-p005 | fix ok | Dynamic Assignment via exec/eval | In rare cases where '__metaclass__ = type' is dynamically assigned via exec or eval in the surrounding scope, removing it could affect name binding in the class's local namespace. |
| r2-p006 | fix ok | Bytes vs Str, Encoding and Newline Handling | bytes vs str, encoding and newline handling, text vs binary modes |
| r2-p007 | fix ok | Trailing Comment on Same Line | The rewrite may remove a '__metaclass__ = type' line that is followed by a trailing comment on the same line, causing the comment to be lost if not handled properly by the autofix. |
| r2-p008 | silent prediction off | Evaluation Order and Side Effects When Merged/Duplicated/Deleted | evaluation order and side effects when expressions are merged, duplicated or deleted |
Sources
r1-p001 · Indentation and Blank Line Preservation Hazard
1class A:
2
3 __metaclass__ = type
4
5 x = 1
6
7print('x:', A.x)
--- orig.py 2026-10-17 17:09:47.806123178 -0400 +++ fixed.py 2026-10-17 17:09:48.097126217 -0400 @@ -1,6 +1,5 @@ class A: - __metaclass__ = type x = 1
x: 1
r1-p002 · Numeric Coercion and Rounding Hazard
1from decimal import Decimal, getcontext
2getcontext().prec = 10
3
4class B:
5 __metaclass__ = type
6 value = Decimal('1') / Decimal('3')
7
8print('value:', float(B.value))
9print('rounded:', round(B.value, 2))
--- orig.py 2026-10-17 17:09:48.199127283 -0400 +++ fixed.py 2026-10-17 17:09:48.384129215 -0400 @@ -2,7 +2,6 @@ getcontext().prec = 10 class B: - __metaclass__ = type value = Decimal('1') / Decimal('3') print('value:', float(B.value))
value: 0.3333333333 rounded: 0.33
r1-p003 · Static Analysis Warning Suppression Hazard
1class C:
2 __metaclass__ = type
3 def method(self): pass
4
5try:
6 C().method()
7 print('method called')
8except Exception as e:
9 print(type(e).__name__ + ':', str(e))
--- orig.py 2026-10-17 17:09:48.497130395 -0400 +++ fixed.py 2026-10-17 17:09:48.660132097 -0400 @@ -1,5 +1,4 @@ class C: - __metaclass__ = type def method(self): pass try:
method called
r1-p004 · Keyword-Only and Defaults Evaluation Hazard
1def make_method(val=[]):
2 def method(self, *, kw=val):
3 return kw
4 return method
5
6class D:
7 __metaclass__ = type
8 method = make_method([])
9
10D().method(kw=99)
11print('default captured:', D().method())
--- orig.py 2026-10-17 17:09:48.764133183 -0400 +++ fixed.py 2026-10-17 17:09:48.933134948 -0400 @@ -4,7 +4,6 @@ return method class D: - __metaclass__ = type method = make_method([]) D().method(kw=99)
default captured: []
r1-p005 · Decorator Evaluation Order Hazard
1@property
2class E:
3 __metaclass__ = type
4 @classmethod
5 def cls_method(cls): return 42
6
7print(E.cls_method)
--- orig.py 2026-10-17 17:09:49.031135972 -0400 +++ fixed.py 2026-10-17 17:09:49.441140253 -0400 @@ -1,6 +1,5 @@ @property class E: - __metaclass__ = type @classmethod def cls_method(cls): return 42
r1-p006 · Intentional Built-in Shadowing Hazard
1class F:
2 __metaclass__ = type
3 t = type
4 print('t is type:', t is type)
5
6print('F.t:', F.t)
--- orig.py 2026-10-17 17:09:49.664142582 -0400 +++ fixed.py 2026-10-17 17:09:49.830144315 -0400 @@ -1,5 +1,4 @@ class F: - __metaclass__ = type t = type print('t is type:', t is type)
t is type: True F.t: <class 'type'>
r1-p007 · Evaluation Order and Side Effects Hazard
1counter = 0
2
3def get_type():
4 global counter
5 counter += 1
6 return type
7
8class G:
9 __metaclass__ = get_type()
10 y = counter
11
12print('counter after class:', counter)
13print('G.y:', G.y)
counter after class: 1 G.y: 1
r1-p008 · Pathlib vs os.path Behavior Hazard
1import os
2
3class H:
4 __metaclass__ = type
5 base = '.'
6 rel = 'test.txt'
7
8with open(rel, 'w') as f:
9 f.write('hello')
10
11try:
12 print('os.path exists:', os.path.exists(H.rel))
13 print('pathlib exists:', os.path.exists(H.base + '/' + H.rel))
14except Exception as e:
15 print(type(e).__name__ + ':', str(e))
16finally:
17 if os.path.exists(H.rel):
18 os.remove(H.rel)
--- orig.py 2026-10-17 17:09:50.075146873 -0400 +++ fixed.py 2026-10-17 17:09:50.469150986 -0400 @@ -1,7 +1,6 @@ import os class H: - __metaclass__ = type base = '.' rel = 'test.txt'
r2-p001 · Code Generation Tool Marker
1# This simulates a codegen tool that looks for __metaclass__ = type
2# to decide whether to inject methods into a class.
3class Target:
4 __metaclass__ = type
5 value = 42
6
7def simulate_codegen(cls):
8 src = cls.__dict__
9 if '__metaclass__ = type' in str(src):
10 return cls.__name__ + '_processed'
11 return cls.__name__ + '_skipped'
12
13print(simulate_codegen(Target))
--- orig.py 2026-10-17 17:10:10.491359623 -0400 +++ fixed.py 2026-10-17 17:10:10.661361391 -0400 @@ -1,7 +1,6 @@ # This simulates a codegen tool that looks for __metaclass__ = type # to decide whether to inject methods into a class. class Target: - __metaclass__ = type value = 42 def simulate_codegen(cls):
Target_skipped
r2-p002 · Placeholder Documentation Artifact
1# This line is intentionally left as a reminder that this class
2# should eventually use a custom metaclass for validation.
3class PendingValidation:
4 __metaclass__ = type # TODO: replace with ValidatingMeta
5 def validate(self):
6 return True
7
8# Simulate a tool that checks for the TODO comment
9has_todo = '__metaclass__ = type' in PendingValidation.__dict__ and 'TODO' in PendingValidation.__dict__['__metaclass__ = type']
10print('Has TODO marker:', has_todo)
--- orig.py 2026-10-17 17:10:10.767362494 -0400 +++ fixed.py 2026-10-17 17:10:10.937364262 -0400 @@ -1,7 +1,6 @@ # This line is intentionally left as a reminder that this class # should eventually use a custom metaclass for validation. class PendingValidation: - __metaclass__ = type # TODO: replace with ValidatingMeta def validate(self): return True
Has TODO marker: False
r2-p003 · String Formatting Edge Cases
1class FormatTest:
2 __metaclass__ = type
3 s = 'hello'
4 # Test various string formatting that might be affected by namespace
5 pct = '%s world' % s
6 fmt = '{} world'.format(s)
7 fstr = f'{s} world'
8 # Ensure these are computed correctly
9 print(repr(pct), repr(fmt), repr(fstr))
10 assert pct == fmt == fstr == 'hello world'
--- orig.py 2026-10-17 17:10:11.044365375 -0400 +++ fixed.py 2026-10-17 17:10:11.216367164 -0400 @@ -1,5 +1,4 @@ class FormatTest: - __metaclass__ = type s = 'hello' # Test various string formatting that might be affected by namespace pct = '%s world' % s
'hello world' 'hello world' 'hello world'
r2-p004 · Exceptions Raised Inside Rewritten Code
1class ExceptionTest:
2 __metaclass__ = type
3 def faulty_method(self):
4 try:
5 x = 1 / 0
6 except ZeroDivisionError as e:
7 try:
8 raise ValueError('bad') from e
9 except ValueError as ve:
10 print('Caught:', type(ve).__name__, str(ve))
11 raise
12 else:
13 print('no error')
14 finally:
15 print('finally')
16
17try:
18 ExceptionTest().faulty_method()
19except ValueError as e:
20 print('Outer caught:', type(e).__name__, str(e))
--- orig.py 2026-10-17 17:10:11.319368235 -0400 +++ fixed.py 2026-10-17 17:10:11.490370013 -0400 @@ -1,5 +1,4 @@ class ExceptionTest: - __metaclass__ = type def faulty_method(self): try: x = 1 / 0
Caught: ValueError bad finally Outer caught: ValueError bad
r2-p005 · Dynamic Assignment via exec/eval
1__metaclass__ = type # This is in module scope
2# Now define a class that relies on this binding being visible
3exec('''
4class DynamicClass:
5 __metaclass__ = type # Should resolve to module __metaclass__
6 attr = 'value'
7''')
8# Check if the class got the attribute
9print('attr in DynamicClass:', hasattr(DynamicClass, 'attr'))
10print('attr value:', getattr(DynamicClass, 'attr', 'missing'))
--- orig.py 2026-10-17 17:10:11.597371126 -0400 +++ fixed.py 2026-10-17 17:10:11.764372862 -0400 @@ -1,4 +1,3 @@ -__metaclass__ = type # This is in module scope # Now define a class that relies on this binding being visible exec(''' class DynamicClass:
attr in DynamicClass: True attr value: value
r2-p006 · Bytes vs Str, Encoding and Newline Handling
1class BytesStrTest:
2 __metaclass__ = type
3 text = 'café'
4 byte_val = text.encode('utf-8')
5 # Write to file in text and binary mode to check encoding
6 with open('test.txt', 'w', encoding='utf-8') as f:
7 f.write(text)
8 with open('test.bin', 'wb') as f:
9 f.write(byte_val)
10 # Read back and verify
11 with open('test.txt', 'r', encoding='utf-8') as f:
12 back_text = f.read()
13 with open('test.bin', 'rb') as f:
14 back_bytes = f.read()
15 print('Text roundtrip:', back_text == text)
16 print('Bytes roundtrip:', back_bytes == byte_val)
17 import os
18 os.remove('test.txt')
19 os.remove('test.bin')
--- orig.py 2026-10-17 17:10:11.866373923 -0400 +++ fixed.py 2026-10-17 17:10:12.043375764 -0400 @@ -1,5 +1,4 @@ class BytesStrTest: - __metaclass__ = type text = 'café' byte_val = text.encode('utf-8') # Write to file in text and binary mode to check encoding
Text roundtrip: True Bytes roundtrip: True
r2-p007 · Trailing Comment on Same Line
1class CommentTest:
2 __metaclass__ = type # This comment must be preserved after the assignment
3 x = 1
4 # We can't directly access the comment, but we can check if the line existed
5 # by seeing if the class still has the attribute (which it shouldn't after fix)
6 # Instead, we verify the comment's intent: the class should behave normally
7 print('x =', x)
8 print('Comment intent preserved: class works')
--- orig.py 2026-10-17 17:10:12.151376887 -0400 +++ fixed.py 2026-10-17 17:10:12.322378664 -0400 @@ -1,5 +1,4 @@ class CommentTest: - __metaclass__ = type # This comment must be preserved after the assignment x = 1 # We can't directly access the comment, but we can check if the line existed # by seeing if the class still has the attribute (which it shouldn't after fix)
x = 1 Comment intent preserved: class works
r2-p008 · Evaluation Order and Side Effects When Merged/Duplicated/Deleted
1side_effect_log = []
2def side_effect_func():
3 side_effect_log.append('called')
4 return type
5
6class SideEffectTest:
7 __metaclass__ = side_effect_func() # This should trigger side effect
8 y = 2
9 # After fix, the line is removed, so side effect should not happen on class definition
10 # But we already called it during class creation
11 print('Side effect log:', side_effect_log)
12 print('Log length:', len(side_effect_log))
Side effect log: ['called'] Log length: 1