OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Check for key in dictionary without magic value comparison [duplicate]

  • Thread starter Thread starter BobVitorBob
  • Start date Start date
B

BobVitorBob

Guest
I'm using pydantic in a project to check file formatting for an object, as well as pylint for linting. Whenever I read a file with an invalid format, I want to raise an exception with a descriptive message. The code I'm using looks something like this:

Code:
# try code...
except pydantic.ValidationError as ve:
    errors = ve.errors(
        include_url=False,
        include_input=False,
        include_context=False,
    )
    messages = []
    for error in errors:
        message = f"{error['type']} at {'.'.join(str(error['loc']))}."
        if "input" in error:
            message = f"{message} Got {error['input']}"
        messages.append(message)
    message = "\n".join(messages)

And it works just fine. My problem is that

Code:
if "input" in error

is a magic value comparison. I understand why, and I know that I could do something like

Code:
VE_ERROR_INPUT_KEY = "input"
for error in errors:
    message = f"{error['type']} at {'.'.join(str(error['loc']))}."
    if VE_ERROR_INPUT_KEY in error:
        message = f"{message} Got {error['input']}"

to "solve" the problem. However, I think that creating a constant for a dict key of a third-party lib to use in a single comparison isn't good practice.

I don't want to turn magic value comparison off, since most of the time I think its good practice, but I also don't want to create a constant for one comparison. My ideal solution is to change how this check is made, but I couldn't find another way. Should I just turn the linter off for that (and other similar) lines?

Edit: I know I can tell pylint to ignore a single line. What I want to know is if there is a better way to find a key inside a dictionary that does not use a magic value comparison, or if this is how it should be done and I should tweak the rule to ignore it.
<p>I'm using <a href="https://docs.pydantic.dev/latest/" rel="nofollow noreferrer">pydantic</a> in a project to check file formatting for an object, as well as <a href="https://pylint.readthedocs.io/en/latest/index.html" rel="nofollow noreferrer">pylint</a> for linting. Whenever I read a file with an invalid format, I want to raise an exception with a descriptive message. The code I'm using looks something like this:</p>
<pre><code># try code...
except pydantic.ValidationError as ve:
errors = ve.errors(
include_url=False,
include_input=False,
include_context=False,
)
messages = []
for error in errors:
message = f"{error['type']} at {'.'.join(str(error['loc']))}."
if "input" in error:
message = f"{message} Got {error['input']}"
messages.append(message)
message = "\n".join(messages)
</code></pre>
<p>And it works just fine. My problem is that</p>
<pre><code>if "input" in error
</code></pre>
<p>is a <a href="https://pylint.readthedocs.io/en/latest/user_guide/messages/refactor/magic-value-comparison.html" rel="nofollow noreferrer">magic value comparison</a>. I understand why, and I know that I could do something like</p>
<pre><code>VE_ERROR_INPUT_KEY = "input"
for error in errors:
message = f"{error['type']} at {'.'.join(str(error['loc']))}."
if VE_ERROR_INPUT_KEY in error:
message = f"{message} Got {error['input']}"
</code></pre>
<p>to "solve" the problem. However, I think that creating a constant for a dict key of a third-party lib to use in a single comparison isn't good practice.</p>
<p><strong>I don't want to turn magic value comparison off</strong>, since most of the time I think its good practice, but I also don't want to create a constant for one comparison. My ideal solution is to change how this check is made, but I couldn't find another way. Should I just turn the linter off for that (and other similar) lines?</p>
<p>Edit: <strong>I know I can tell pylint to ignore a single line</strong>. What I want to know is if there is a better way to find a key inside a dictionary that does not use a magic value comparison, or if this is how it should be done and I should tweak the rule to ignore it.</p>
 

Latest posts

Top