Repository: koxudaxi/datamodel-code-generator

Issue:
typename__ Field does not honor the `--use-default-kwarg` flag.
First off, thanks for making an awesome and easy to use tool!

**Describe the bug**

I use this tool to generate pydantic v2 models from graphql code. I am using the `--use-default-kwarg` flag to ensure that pydantic fields specify default as a keyword argument (mainly to comply with some silly pylance type hint issues otherwise). 

when using this flag, I find that while all defined fields of a given class _do_ comply with this configuration, the always present `typename__` does **not** comply.   

**To Reproduce**

Example schema:

**tmp.bug.graphql**

```graphql
type TestType {
  "This is an ID field that is not required."
  typeId: String!
  "This is a description field that is optional."
  typeDescription: String
}
```


```python
# generated by datamodel-codegen:
#   filename:  tmp.bug.graphql
#   timestamp: 2025-06-13T00:16:39+00:00

from __future__ import annotations

from typing import Literal, Optional, TypeAlias

from pydantic import BaseModel, Field

Boolean: TypeAlias = bool
"""
The `Boolean` scalar type represents `true` or `false`.
"""


String: TypeAlias = str
"""
The `String` scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text.
"""


class TestType(BaseModel):
    type_description: Optional[String] = Field(
        default=None,
        alias='typeDescription',
        description='This is a description field that is optional.',
    )
    type_id: String = Field(
        ..., alias='typeId', description='This is an ID field that is not required.'
    )
    typename__: Optional[Literal['TestType']] = Field('TestType', alias='__typename')

```

command that was used:
```bash
datamodel-codegen \
    --input tmp.bug.graphql  \
    --input-file-type graphql \
    --output-model-type pydantic_v2.BaseModel \
    --output tmp_bug.py \
    --target-python-version 3.11 \
    --use-schema-description \
    --snake-case-field \
    --use-default-kwarg
```

**Expected behavior**

I would have expected the `typename__` field to also specify default using keyword argument. 

```python
class TestType(BaseModel):
    type_description: Optional[String] = Field(
        default=None,
        alias='typeDescription',
        description='This is a description field that is optional.',
    )
    type_id: String = Field(
        ..., alias='typeId', description='This is an ID field that is not required.'
    )
    typename__: Optional[Literal['TestType']] = Field(default='TestType', alias='__typename')

```

**Version:**
 - OS: `macOS 15.4`
 - Python version: `3.11`
 - datamodel-code-generator version: `0.30.2`





Discussion:

Patch:
diff --git a/src/datamodel_code_generator/parser/graphql.py b/src/datamodel_code_generator/parser/graphql.py
index 5db5db6ef..866959d9c 100644
--- a/src/datamodel_code_generator/parser/graphql.py
+++ b/src/datamodel_code_generator/parser/graphql.py
@@ -308,6 +308,7 @@ def _typename_field(self, name: str) -> DataModelFieldBase:
             required=False,
             alias="__typename",
             use_one_literal_as_default=True,
+            use_default_kwarg=self.use_default_kwarg,
             has_default=True,
         )
 


Test patch:
diff --git a/tests/data/expected/main/graphql/annotated_use_default_kwarg.py b/tests/data/expected/main/graphql/annotated_use_default_kwarg.py
new file mode 100644
index 000000000..84df1a380
--- /dev/null
+++ b/tests/data/expected/main/graphql/annotated_use_default_kwarg.py
@@ -0,0 +1,31 @@
+# generated by datamodel-codegen:
+#   filename:  annotated.graphql
+#   timestamp: 2019-07-26T00:00:00+00:00
+
+from __future__ import annotations
+
+from typing import List, Literal, Optional, TypeAlias
+
+from pydantic import BaseModel, Field
+
+Boolean: TypeAlias = bool
+"""
+The `Boolean` scalar type represents `true` or `false`.
+"""
+
+
+String: TypeAlias = str
+"""
+The `String` scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text.
+"""
+
+
+class A(BaseModel):
+    field: String
+    listField: List[String]
+    listListField: List[List[String]]
+    listOptionalField: List[Optional[String]]
+    optionalField: Optional[String] = None
+    optionalListField: Optional[List[String]] = None
+    optionalListOptionalField: Optional[List[Optional[String]]] = None
+    typename__: Optional[Literal['A']] = Field(default='A', alias='__typename')
diff --git a/tests/main/graphql/test_main_graphql.py b/tests/main/graphql/test_main_graphql.py
index e78f31f71..bef78f067 100644
--- a/tests/main/graphql/test_main_graphql.py
+++ b/tests/main/graphql/test_main_graphql.py
@@ -78,6 +78,23 @@ def test_main_graphql_different_types_of_fields() -> None:
         assert output_file.read_text() == (EXPECTED_GRAPHQL_PATH / "different_types_of_fields.py").read_text()
 
 
+@freeze_time("2019-07-26")
+def test_main_use_default_kwarg() -> None:
+    with TemporaryDirectory() as output_dir:
+        output_file: Path = Path(output_dir) / "output.py"
+        return_code: Exit = main([
+            "--input",
+            str(GRAPHQL_DATA_PATH / "annotated.graphql"),
+            "--output",
+            str(output_file),
+            "--input-file-type",
+            "graphql",
+            "--use-default-kwarg",
+        ])
+        assert return_code == Exit.OK
+        assert output_file.read_text() == (EXPECTED_GRAPHQL_PATH / "annotated_use_default_kwarg.py").read_text()
+
+
 @freeze_time("2019-07-26")
 @pytest.mark.skipif(
     black.__version__.split(".")[0] == "19",