Is there any way around PyQt Signal type coercion? /u/Magg0tBrainz Python Education

https://stackoverflow.com/questions/46973384/pyqt5-wrong-conversion-of-int-object-when-using-custom-signal/46975980

When sending signals, PyQt will coerce the data sent in the signal to the type that the signal was declared with.

If I declare signal = Signal(int), and then do self.signal.emit('myString'), it will attempt to coerce myString into an int, without throwing any errors or warnings.

This is incredibly annoying. There have been so many times where I’ve been debugging a program for hours, and then start grasping at straws and checking all the abstracted away parts of the code, which leads to me trying to just set the signal type to `Signal(Any)`, and then it suddenly works like magic. Maybe that’s partly on me, and I should just learn to check these things sooner, but still.

I don’t want the signal to change what I’m emitting. I created the signal with a specific type for a reason, and I would expect it to tell me if I’m using it wrong, so that I can quickly correct my mistake. Why else would I declare the type of my signal? This is why I declare the type of literally anything, for debugging and type hinting.

Is there any way around this behaviour?

——————————————–

I tried making a StrictSignal class that inherits from the Signal class but overrides the emit function to make it perform a type check first before emitting, but I couldn’t quite figure out how to make it work properly.

from typing import Type from PySide6.QtCore import Signal class StrictSignal(Signal): def __init__(self, *types: Type): super().__init__(*types) self._types = types def emit(self, *args): if len(args) != len(self._types): raise TypeError(f"Expected {len(self._types)} arguments, got {len(args)}") for arg, expected_type in zip(args, self._types): if not isinstance(arg, expected_type): raise TypeError(f"Expected type {expected_type} for argument, got {type(arg)}") super().emit(*args) 

I get the following error:

Cannot access attribute “emit” for class “object”
Attribute “emit” is unknown

Though I think that is more just me being stupid than a PyQt issue.

I also have doubts about whether my solution would otherwise operate normally as a Signal object, properly inheriting the connect and disconnect functions.

submitted by /u/Magg0tBrainz
[link] [comments]

​r/learnpython https://stackoverflow.com/questions/46973384/pyqt5-wrong-conversion-of-int-object-when-using-custom-signal/46975980 When sending signals, PyQt will coerce the data sent in the signal to the type that the signal was declared with. If I declare signal = Signal(int), and then do self.signal.emit(‘myString’), it will attempt to coerce myString into an int, without throwing any errors or warnings. This is incredibly annoying. There have been so many times where I’ve been debugging a program for hours, and then start grasping at straws and checking all the abstracted away parts of the code, which leads to me trying to just set the signal type to `Signal(Any)`, and then it suddenly works like magic. Maybe that’s partly on me, and I should just learn to check these things sooner, but still. I don’t want the signal to change what I’m emitting. I created the signal with a specific type for a reason, and I would expect it to tell me if I’m using it wrong, so that I can quickly correct my mistake. Why else would I declare the type of my signal? This is why I declare the type of literally anything, for debugging and type hinting. Is there any way around this behaviour? ——————————————– I tried making a StrictSignal class that inherits from the Signal class but overrides the emit function to make it perform a type check first before emitting, but I couldn’t quite figure out how to make it work properly. from typing import Type from PySide6.QtCore import Signal class StrictSignal(Signal): def __init__(self, *types: Type): super().__init__(*types) self._types = types def emit(self, *args): if len(args) != len(self._types): raise TypeError(f”Expected {len(self._types)} arguments, got {len(args)}”) for arg, expected_type in zip(args, self._types): if not isinstance(arg, expected_type): raise TypeError(f”Expected type {expected_type} for argument, got {type(arg)}”) super().emit(*args) I get the following error: Cannot access attribute “emit” for class “object” Attribute “emit” is unknown Though I think that is more just me being stupid than a PyQt issue. I also have doubts about whether my solution would otherwise operate normally as a Signal object, properly inheriting the connect and disconnect functions. submitted by /u/Magg0tBrainz [link] [comments] 

https://stackoverflow.com/questions/46973384/pyqt5-wrong-conversion-of-int-object-when-using-custom-signal/46975980

When sending signals, PyQt will coerce the data sent in the signal to the type that the signal was declared with.

If I declare signal = Signal(int), and then do self.signal.emit('myString'), it will attempt to coerce myString into an int, without throwing any errors or warnings.

This is incredibly annoying. There have been so many times where I’ve been debugging a program for hours, and then start grasping at straws and checking all the abstracted away parts of the code, which leads to me trying to just set the signal type to `Signal(Any)`, and then it suddenly works like magic. Maybe that’s partly on me, and I should just learn to check these things sooner, but still.

I don’t want the signal to change what I’m emitting. I created the signal with a specific type for a reason, and I would expect it to tell me if I’m using it wrong, so that I can quickly correct my mistake. Why else would I declare the type of my signal? This is why I declare the type of literally anything, for debugging and type hinting.

Is there any way around this behaviour?

——————————————–

I tried making a StrictSignal class that inherits from the Signal class but overrides the emit function to make it perform a type check first before emitting, but I couldn’t quite figure out how to make it work properly.

from typing import Type from PySide6.QtCore import Signal class StrictSignal(Signal): def __init__(self, *types: Type): super().__init__(*types) self._types = types def emit(self, *args): if len(args) != len(self._types): raise TypeError(f"Expected {len(self._types)} arguments, got {len(args)}") for arg, expected_type in zip(args, self._types): if not isinstance(arg, expected_type): raise TypeError(f"Expected type {expected_type} for argument, got {type(arg)}") super().emit(*args) 

I get the following error:

Cannot access attribute “emit” for class “object”
Attribute “emit” is unknown

Though I think that is more just me being stupid than a PyQt issue.

I also have doubts about whether my solution would otherwise operate normally as a Signal object, properly inheriting the connect and disconnect functions.

submitted by /u/Magg0tBrainz
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *