Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve edge case validation for GeneralConfig.get_num_threads #22

Open
ashsong-nv opened this issue Nov 27, 2024 · 0 comments
Open

Improve edge case validation for GeneralConfig.get_num_threads #22

ashsong-nv opened this issue Nov 27, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@ashsong-nv
Copy link
Collaborator

There is a minor input validation bug for src/cve/data_models/config.py GeneralConfig.get_num_threads:

The field validator appears to expect invalid values:

    @field_validator("num_threads")
    @classmethod
    def get_num_threads(cls, v: int) -> int:
        if v is None or v < 1:
            return os.cpu_count() or 1

        return v

However this runs after Pydantic's own PositiveInt validator. So the validator effectively does nothing and GeneralConfig(num_threads=None) will raise a ValidationError. Setting @field_validator("num_threads", mode="before") fixes the issue.

The reason we need the field validator:

  • os.cpu_count() sometimes returns None, the validator will supply a value of 1 in that situation
  • The validator will replace values of None and anything <1 with os.cpu_count()
@ashsong-nv ashsong-nv added the bug Something isn't working label Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant