attrs icon indicating copy to clipboard operation
attrs copied to clipboard

Question: Type Annotations and Docs

Open OzupeSir opened this issue 3 months ago • 1 comments

I am learning to use the attrs package and I have two questions to ask:

  1. When using 'converter' to modify the input type when creating a property, it will cause IDE to make an error in judging the input type. For example, I originally wanted this input to be int, but it prompted me that it should be str. Is there any good way to solve this problem?
Image
  1. When using the class xxxx to create traditional classes, it is common to add instantiation comments in the __init__ method. Now, with attrs, the instantiation process no longer has parameter prompts. Is there any other good way to solve the problem of document prompts during instantiation besides adding comments directly to the class?
Image Image
import attrs


@attrs.define
class Com:
    """
    Initialize Com2 instance.

    :param code: Code of the Com2 instance.
    """
    code: str = attrs.field(converter=str)

class Com2:
    def __init__(self, code: int):
        """
        Initialize Com2 instance.

        :param code: Code of the Com2 instance.
        """
        self.code: str = str(code)

if __name__ == '__main__':
    com = Com(code=1)

    com2 = Com2(code=2)

OzupeSir avatar Nov 03 '25 10:11 OzupeSir

  1. I'm fairly certain there's no straightforward solution as of right now
  2. Use "var" instead of "param"

nwolf721 avatar Nov 22 '25 15:11 nwolf721