Skip to content

Allow construction of const_class with strict types and kwargs #14

@whiskeyo

Description

@whiskeyo

With such code:

import constclasses as cc

@cc.const_class(with_strict_types=True)
class Widget:
    first: int
    second: int

widget = Widget(first=10, second=20)

Python throws an error constclasses.ccerror.InitializationError: Invalid number of arguments: expected 2 - got 0.

Extending the const_class decorator with with_kwargs=True in such way:

import constclasses as cc

@cc.const_class(with_strict_types=True, with_kwargs=True)
class Widget:
    first: int
    second: int

widget = Widget(first=10, second=20)

seems to solve the problem, but then the Widget cannot be created without using named arguments, e.g. with widget = Widget(1, 2), since it throws:

E   TypeError: Attribute value does not match the declared type:
E       attribute: first, declared type: <class 'int'>, actual type: <class 'NoneType'>

Could the constclasses.const_class be somehow extended to support both named and unnamed arguments, so the same class can be reused?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions