pyiron / uniton

BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Append type hinting to the class #8

Closed samwaseda closed 1 month ago

samwaseda commented 1 month ago

Following this discussion I finally realized what I had in mind, even though I have to admit it might not work exactly in the way I wish it would.

For this class:

from dataclasses import dataclass
from typing import Annotated

@dataclass
class Pizza:
    price: Annotated[float, "money"]
    size: Annotated[float, "dimension"]

    @dataclass
    class Topping:
        sauce: Annotated[str, "matter"]

I get the following behaviour:

from uniton.converter.append_types

append_types(Pizza)

print(Pizza)
print(Pizza.Topping)
print(Pizza.size)
print(Pizza.price)
print(Pizza.Topping.sauce)

Output:

<class '__main__.Pizza'>
<class '__main__.Pizza.Topping'>
typing.Annotated[float, 'dimension']
typing.Annotated[float, 'money']
typing.Annotated[str, 'matter']

The only one problem is that the default value is lost when there is one and it's accessed without instantiating the class, i.e.

@dataclass
class Pizza:
    size: Annotated[float, "dimension"] = 10

print(Pizza.size)
append_types(Pizza)
print(Pizza.size)

Output:

10
typing.Annotated[float, 'dimension']

But it doesn't really interfere with any functionality if it's instantiated. This being said, I would have loved to do it without overwriting the class, but I couldn't find any viable solution other than this one.

github-actions[bot] commented 1 month ago

Binder :point_left: Launch a binder notebook on branch _pyiron/uniton/scrape_typehints

codacy-production[bot] commented 1 month ago

Coverage summary from Codacy

See diff coverage on Codacy

Coverage variation Diff coverage
:white_check_mark: +0.93% (target: -1.00%) :white_check_mark: 100.00%
Coverage variation details | | Coverable lines | Covered lines | Coverage | | ------------- | ------------- | ------------- | ------------- | | Common ancestor commit (9b725f3645d98a205acc6b68b53972c3ea9597cd) | 55 | 53 | 96.36% | | | Head commit (6e0d38e3b232670a1a0e6e88a7e00e3aa2efdd9b) | 74 (+19) | 72 (+19) | 97.30% (**+0.93%**) | **Coverage variation** is the difference between the coverage for the head and common ancestor commits of the pull request branch: ` - `
Diff coverage details | | Coverable lines | Covered lines | Diff coverage | | ------------- | ------------- | ------------- | ------------- | | Pull request (#8) | 9 | 9 | **100.00%** | **Diff coverage** is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: `/ * 100%`

See your quality gate settings    Change summary preferences

Codacy stopped sending the deprecated coverage status on June 5th, 2024. Learn more

coveralls commented 1 month ago

Pull Request Test Coverage Report for Build 11086794417

Details


Totals Coverage Status
Change from base Build 11076924624: 0.4%
Covered Lines: 72
Relevant Lines: 74

💛 - Coveralls