Skip to content

validation triggered too late #461

@IaroslavR

Description

@IaroslavR

attrs 18.2.0
python 3.6.7
I'm not sure, is it bug or expected behavior, but steps to reproduce:

import attr


def validate_x(_, __, value):
    if value not in ["a", "b", ]:
        raise ValueError(f"Unknown type: {value}")


@attr.s(auto_attribs=True)
class A:
    name: str = attr.ib(validator=validate_x)
    map: dict = {
        "x": 1,
        "y": 2,
    }
    detector: str = attr.ib()

    @detector.default
    def init_detector(self):
        return self.map[self.name]


a = A('c')

Expected(by me):

Traceback (most recent call last):
  File "/home/mirror/tst.py", line 23, in <module>
    a = A('c')
  File "<attrs generated init 090efc2f40404e910a622fb3a7382642c4cc7dba>", line 9, in __init__
  File "/home/mirror/tst.py", line 6, in validate_x
    raise ValueError(f"Unknown type: {value}")
ValueError: Unknown type: c

Real:

Traceback (most recent call last):
  File "/home/mirror/tst.py", line 23, in <module>
    a = A('c')
  File "<attrs generated init b58e3f79027d88b47fd9d82dccbb4d2f365e3754>", line 7, in __init__
  File "/home/mirror/tst.py", line 20, in init_detector
    return self.map[self.name]
KeyError: 'c'

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions