Everytime I have to make a small python tool available to my teammembers it really drives home how crazy it is because everything is realistically one of one or two types, and have to document it as such and build in a thousand little statements to make sure other people don't feed in the wrong type. What's madening is that how python does type checking changes every so often, and libraries are sometimes incosistent with how they name their types when borrowing classes from other libraries.
It's funny how Python spent the past like 6 years developing the type hinting system (which is a massive improvement from no typing don't get me wrong) only to start re-introducing duck typing again with the Protocols.
The one thing I'll always love about it is the ability to override builtins and do dumb things like call 'sum(cats)' where cats is a list of cat objects and have it like print out meow to the terminal for each cat in the list.
Shit like that makes it perfect for internal tools.
Even better is to create a decorator and just wrap the offending functions:
defshut_up(func):
defcall(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as e:
print(f"shit's fucked, but I'll be quiet about it")
returnreturn call
@shut_updefadd(x: int, y: int):
print(x + y)
add(1, 2)
add(-1, 2)
add(1, "2")
>>> 3>>> 1>>> "shit's fucked, but I'll be quiet about it"
Or if you want to attempt to salvage it:
defshut_up(func):
defcall(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as e:
try:
return func(*map(int, args), **kwargs)
except Exception as e:
print(f"shit's really fucked, I even tried to fix it for you")
returnNonereturn call
Everytime I have to make a small python tool available to my teammembers it really drives home how crazy it is because everything is realistically one of one or two types, and have to document it as such and build in a thousand little statements to make sure other people don't feed in the wrong type. What's madening is that how python does type checking changes every so often, and libraries are sometimes incosistent with how they name their types when borrowing classes from other libraries.
It's funny how Python spent the past like 6 years developing the type hinting system (which is a massive improvement from no typing don't get me wrong) only to start re-introducing duck typing again with the Protocols.
The one thing I'll always love about it is the ability to override builtins and do dumb things like call 'sum(cats)' where cats is a list of cat objects and have it like print out meow to the terminal for each cat in the list.
Shit like that makes it perfect for internal tools.
At some point I'm just gonna give up and do
Try:
Entire fucking program
Except:
Print("error: something went wrong")
Even better is to create a decorator and just wrap the offending functions:
def shut_up(func): def call(*args, **kwargs): try: return func(*args, **kwargs) except Exception as e: print(f"shit's fucked, but I'll be quiet about it") return return call @shut_up def add(x: int, y: int): print(x + y) add(1, 2) add(-1, 2) add(1, "2") >>> 3 >>> 1 >>> "shit's fucked, but I'll be quiet about it"
Or if you want to attempt to salvage it:
def shut_up(func): def call(*args, **kwargs): try: return func(*args, **kwargs) except Exception as e: try: return func(*map(int, args), **kwargs) except Exception as e: print(f"shit's really fucked, I even tried to fix it for you") return None return call