Skip to content

feat(sdk/python): normalize names to avoid escape hatch for reserved keywords - #8014

Merged
sipsma merged 2 commits into
dagger:mainfrom
helderco:sdk/python/normalize-names
Jul 25, 2024
Merged

sipsma merged 2 commits into
dagger:mainfrom
helderco:sdk/python/normalize-names

Conversation

@helderco

@helderco helderco commented Jul 23, 2024 •

Copy link
Copy Markdown
Contributor

Before

Name overrides were created as an escape hatch to allow using function, attribute, and parameter names that conflict with Python reserved words:

from typing import Annotated
import dagger

@dagger.object_type
class Test:
    from_: str = dagger.field(name="from")

    @dagger.function(name="with")
    def with_(self, import_: Annotated[str, dagger.Arg("import")] = "") -> str:
        return import_

After

Since the convention in these cases is to add an underscore to the end, we can remove it automatically when registering the name with Dagger:

import dagger

@dagger.object_type
class Test:
    from_: str = dagger.field()

    @dagger.function
     def with_(self, import_: str = "") -> str:
         return import_

This avoids the need to override the name in a way that's not documented.

It applies to names that only have a single underscore in the end, so we don't make too many assumptions about what's intended. The escape hatch is still available for now in any case, but might get deprecated at some point, because not all SDKs have it.

@helderco
helderco requested review from TomChv and jedevc July 23, 2024 13:21
@helderco
helderco force-pushed the sdk/python/normalize-names branch 2 times, most recently from a8e515d to 6febbd7 Compare July 23, 2024 14:29
@helderco helderco added this to the v0.12.3 milestone Jul 23, 2024
@helderco
helderco force-pushed the sdk/python/normalize-names branch 2 times, most recently from 979e68d to aa8c55f Compare July 24, 2024 10:37
helderco added 2 commits July 25, 2024 09:12
…erride

Name overrides were created as an escape hatch to allow using function, attribute, and parameter names that conflict with Python reserved words.

Since the convention in these cases is to add an underscore to the end, we can remove it automatically when registering the name with Dagger. This avoids the need to override the name in a way that's not documented.

This applies to names that only have a single underscore in the end, so we don't make too many assumptions about what's intended. The escape hatch is still available for now in any case, but might get deprecated at some point, because not all SDKs have it.

Signed-off-by: Helder Correia <[email protected]>
Signed-off-by: Helder Correia <[email protected]>
@helderco
helderco force-pushed the sdk/python/normalize-names branch from aa8c55f to 4f16802 Compare July 25, 2024 09:12
@sipsma
sipsma merged commit 9955dce into dagger:main Jul 25, 2024
@helderco
helderco deleted the sdk/python/normalize-names branch July 25, 2024 18:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants