mirror of
https://github.com/github/codeql.git
synced 2026-04-28 18:25:24 +02:00
Update docs
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
class A:
|
||||
def __init__(self, a):
|
||||
self.a = a
|
||||
|
||||
def __add__(self, other):
|
||||
# BAD: Should return NotImplemented instead of raising
|
||||
if not isinstance(other,A):
|
||||
raise TypeError(f"Cannot add A to {other.__type__}")
|
||||
return A(self.a + other.a)
|
||||
|
||||
class B:
|
||||
def __init__(self, a):
|
||||
self.a = a
|
||||
|
||||
def __add__(self, other):
|
||||
# GOOD: Returning NotImplemented allows for other classes to support adding do B.
|
||||
if not isinstance(other,B):
|
||||
return NotImplemented
|
||||
return B(self.a + other.a)
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
class C:
|
||||
def __getitem__(self, idx):
|
||||
if self.idx < 0:
|
||||
# BAD: Should raise a KeyError or IndexError instead.
|
||||
raise ValueError("Invalid index")
|
||||
return self.lookup(idx)
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
class D:
|
||||
def __hash__(self):
|
||||
# BAD: Use `__hash__ = None` instead.
|
||||
raise NotImplementedError(f"{self.__type__} is unhashable.")
|
||||
Reference in New Issue
Block a user