fix: move names definition to class-level#913
Conversation
ResonCode documentation states: > See ReasonCode.names for a list of possible numeric values > along with their names and the packets to which they apply. Signed-off-by: Pau Figueras <pau.figueras@asensenova.com>
|
Looks like I made a duplicate of #845 , but that PR has more changes. |
|
I completely understand where you're coming from, but it would be simpler to change the documentation, than to change the code. My concern is that mutations to the dict, would now affect all instances' dicts. I don't know why a user would ever do that, but that's no reason to break their code. More importantly, it would cause test pollution if that dict was mutated in testing (it would then affect that dict in other tests). My first request is simply to rephrase the docstring, and any other references to it. If there are still good reasons for this change, then my second request is to making the class variable |
|
My reason for this change was to see the list of I agree changing the docs is easier, since I assume creating Should I close this PR? I can take care of updating the docs referencing this part. |
|
I think having ReasonCode.name visible is very useful. But if that mean's it's a class variable, it would just be even better if it was a read only one. This is what I'd do: import functools
import warnings
from typing import Any
from types import MappingProxyType
from .packettypes import PacketTypes
@functools.total_ordering
class ReasonCode:
"""MQTT version 5.0 reason codes class.
See ReasonCode.names for a list of possible numeric values along with their
names and the packets to which they apply.
"""
names = MappingProxyType({
0: {
"Success": [PacketTypes.CONNACK, PacketTypes.PUBACK, PacketTypes.PUBREC, PacketTypes.PUBREL, PacketTypes.PUBCOMP, PacketTypes.UNSUBACK, PacketTypes.AUTH],
"Normal disconnection": [PacketTypes.DISCONNECT],
"Granted QoS 0": [PacketTypes.SUBACK],
},
1: { ... },
...
})
I tried to click the file in browser and make a suggestion it that way. But I ended up adding a PR on top of your PR (how are you two creating these suggestions - have I just not got permissions to do it?). |
|
I can see that. I did not know about this Also to add a suggestion, when you have selected a line or a few lines on the code review, there a button that looks like a file and has a «±» in the middle. Click it and it adds the suggestion. |
|
Nice one Pau - thankyou :). I was wondering how to do exactly that. |
But therefore the option of updating documentation won't solve this problem ? Since you need a instance of the class to access those attributes. Since the value are "constant", I do think they belong to class attributes (not instance attribute as they correctly do). But I agree that mutation could be an issue: if someone previously mutated the instance attributes, thinking it would be safe since it's instance attribute, our change shouldn't impact globally. So we should ensure user don't mutate by mistake the global values. I never user MQTT v5 & Properties, but I don't known if the format exported by As minimal step, I suggest:
What do you think about this suggestion ? |
|
Making a mutable instance variable from a read only class variable sounds like the best of both worlds to me. |
Make the class attribute immutable using a `MappingProxyType`, as suggested, and mask it a mutable instance class.
|
Changes applied. As a side note, IMO the |
|
Also, should I merge main to fix the lint checks? |
|
The lint check is failing due to this mypy error: I'll take a look. Hopefully it's a straightforward annotation. |
|
Alr thanks @JamesParrott , it looks like shadowing |
ResonCode documentation states:
Make the code comply with the docs.