Skip to content

Conversation

@m0nkeyc0de
Copy link
Contributor

Fixes: #695

Validation of filter() kwargs is optionnaly done in pynetbox:

  • it can be enabled globally with strict_filters=True when instanciating the API object
  • it can be enabled/disabled per request by setting strict_filters on the filter() or get() methods
  • if enabled a ParameterValidationError is raised
  • if disabled a line is printed in stdout

@jnovinger
Copy link
Member

@m0nkeyc0de , looks like your changes have some linter errors.

@m0nkeyc0de
Copy link
Contributor Author

@jnovinger, my bad, I'm too used to pre-commit hooks who do that automagically

Linting is done in commit 73d9821

@m0nkeyc0de
Copy link
Contributor Author

@jnovinger code updated regarding your comments (except for the yet unresolved conversation)

I also mentionned the purpose of strict_filters in the README file.

jnovinger
jnovinger previously approved these changes Jun 17, 2025
Copy link
Member

@jnovinger jnovinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, @m0nkeyc0de, thanks for the iteration and improvements!

I appreciate the separation of the strict validation tests in to a separate test class. Also, I did leave one comment on a completely optional approach to setting up mocks for the strict validation tests--happy to merge this with or without that.

def test_filter_strict_valid_kwargs(self):
with patch(
"pynetbox.core.query.Request._make_call", return_value=Mock()
) as mock:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is totally optional, but you can also use patch as a function/method or class decorator, allowing you to reduce boilerplate and indentation in test code.

It would look something like this:

@patch("pynetbox.core.query.Request._make_call", return_value=Mock())
class StrictFilterTestCase(unittest.TestCase):
    def test_filter_strict_valid_kwargs(self, mock):
        # the rest of the test
        ...

Again, totally optional, but thought it was worth sharing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for sharing this trick. I figured out that I wasn't using this mock variable as I'm only watching for the Exception beeing raised or not in my tests.

This unused boilerplate is now removed.

@jnovinger jnovinger merged commit 97f1e8b into netbox-community:master Jun 23, 2025
9 checks passed
@m0nkeyc0de m0nkeyc0de deleted the filter-validation branch June 23, 2025 14:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Filter arguments validation

2 participants