#Python `__init__()`

1 messages ยท Page 1 of 1 (latest)

lavish mortar
#

Hey @vague steppe

#

You don't need to create a __init__() because @object_type is a wrapper around @dataclass, which generates a __init__ automatically. All instance attributes are a part of that constructor by default, unless you specify init=False.

#

So re: #python message, it's because you override __init__ that those arguments then don't get registered in Dagger.

vague steppe
#

ok, so I should remove the __init__ and where should I put my common args in my class ? (sorry for the stupid question)

lavish mortar
#

Not stupid at all, I'm here to help! ๐Ÿ™‚ Can you clarify what you mean by "common args"?

vague steppe
#
dagger -m ./ci-tools/dagger/functions/python/ call --help
Setup tracing at https://dagger.cloud/traces/setup. To hide: export STOPIT=1

โœ” connect 1.5s
โœ” initialize 12.0s
โœ” prepare 0.0s

Call a module function

USAGE
  dagger call [options] [arguments] <function>

FUNCTIONS
  build-oci     Build an Open Container Initiative
  push          Trigger the push of the image you have to call before the 'build-oci' function
  serve         Expose your service
  test          Execute tests

ARGUMENTS
      --pipeline-id string                Kind of namespace between different projects [required]
      --source Directory                  Source directory [required]
      --build-system-packages strings     
      --exclude-directories strings       
      --exclude-files strings             
      --poetry-version string             The poetry version to use (default "1.8.3")
      --python-version string             The python version to use (default "3.12.5")
      --runtime-system-packages strings   
#

when I'm calling the help on my module without functions

#

in golang I'm setting them as arg in the new function

lavish mortar
#

Ah! So, just change the default values to be instances of the dagger.field() descriptor. Example:

- python_version: Annotated[str, Doc("The python version to use")] = "3.12.5",
+ python_version: Annotated[str, Doc("The python version to use")] = dagger.field(default="3.12.5"),

So dagger.field() basically exposes a getter function so it shows under FUNCTIONS in dagger call --help. Without it it'll only be used as an argument to the constructor. You can also expose it as a function, without using it in the constructor by passing dagger.field(init=False). And if you don't want to expose a function nor create a constructor argument, then you use dataclasses.field(init=False).

vague steppe
#

ok I test that

lavish mortar
vague steppe
#

it seems better thank you I have another issue but I thik more related to python code I try to fix that and keep you in touch if everything is ok ๐Ÿ™‚