question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sub class validator behaviour loses type.

See original GitHub issue

There’s some non-obvious behaviour that loses types in pydantic in the following case.

    import pydantic
    
    class IntSub(int):
        pass

    class IntConf(pydantic.BaseModel):
        count: IntSub
    
    c = IntSub(42)
    x = IntConf(count=c)
    assert type(x.count) is IntSub

This is because find_validators() checks whether count is a subclass of int and if it is returns a (int, number_size_validator) pair of validators. So upon creation x.count is set to int©.

This could be fixed by replacing the int() validator with the following:

def new_int_validator(x):
    try:
        int(x)
    except ValueError:
        raise SomeDescriptiveError("blah blah")
    return x

This is more strongly typed and since we appear to be caring about typing if we’re using pydantic, better … I believe. But this is a breaking change since things like IntSub(3.14) and IntSub(“2”) will stop working.

Same goes for float subtypes … I assume.

An alternative is to require a cast constructor for subclasses and use that to check the type instead, e.g. consider what pydantic does without the explicit c=IntSub(42) step above. So perhaps a better check is that the input is already of the expected type… if not try a cast constructor, if that fails we tried.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
samuelcolvincommented, Apr 3, 2018

Humm, I see the problem, but I definitely want 3.14 and "2" to work with int fields.

It looks like it’s not properly documented by the solution is to use get_validators like constr does.

You’ll want something like

class IntSub(int):
    @classmethod
    def get_validators(cls):
        yield cls.myvalidator

    @classmethod
    def myvalidator(cls, v):
        return cls(v)

Let me know if that works.

0reactions
samuelcolvincommented, Apr 16, 2018

fix via #157

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sub class validator behaviour loses type. · Issue #147 - GitHub
There's some non-obvious behaviour that loses types in pydantic in the following case. ... This is because find_validators() checks whether count ...
Read more >
Validate Property Values - MATLAB & Simulink - MathWorks
The validation applies to all subclasses that implement the property. However, subclasses cannot use any validation on their implementation of the property.
Read more >
class-validator relational validation with custom error messages
So here is the code for customizing error message based on the type and I used defaultMessage() to customize it. validate-value-by-type.
Read more >
Data validation in WPF - Magnus Montin
A custom validation rule is a class that derives from the abstract System.Windows.Controls.ValidationRule class and implements its Validate ...
Read more >
Data binding overview - WPF .NET - Microsoft Learn
You may want your app to enable users to change the data and propagate it back to the source object. Or you may...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found