Type annotation in python-attrs

Viewed 22

I'm going to use attrs for a class within my ETL process and currently I struggle with its type annotation capabilities.

For instance, I have this code:

@define(auto_attribs=True, kw_only=True)
class MyDataClass:
    package_name: str
    current_number: int
    start: int
    end: int
    @classmethod
    def from_file(cls, filename):
        (package_name, current_number, start, end) = os.path.basename(filename).split('_')
        return cls(package_name=package_name, current_number=current_number, start=start, end=end)

The filename looks like this: foobar_20220001_20220911_20220912

So, from my point of view part 2, 3 and 4 would be numbers, however when I create an instance of that class everything looks like strings:

>>> m = MyDataClass.from_file('foobar_20220001_20220911_20220912')
>>> print(m)
MyDataClass(package_name='foobar', current_number='20220001', from='20220911', to='20220912')

Obviously strings are used instead of int. do I really have to use converters to get integers for the specific fields? I thought that type annotation should be enough to specify the type.

0 Answers
Related