Skip to content

Commit eb35848

Browse files
committed
implement final
1 parent cf07b83 commit eb35848

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/py_avro_schema/_alias.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import dataclasses
99
from collections import defaultdict
10-
from typing import Annotated, Type, get_args, get_origin
10+
from typing import Annotated, Final, Type, get_args, get_origin
1111

1212
FQN = str
1313
"""Fully qualified name for a Python type"""
@@ -108,6 +108,10 @@ def get_field_aliases_and_actual_type(py_type: Type) -> tuple[list[str] | None,
108108
Check if a type contains an alias metadata via `Alias` or `Aliases` as metadata.
109109
It returns the eventual aliases and the type.
110110
"""
111+
112+
if get_origin(py_type) is Final:
113+
return [], get_args(py_type)[0]
114+
111115
# py_type is not annotated. It can't have aliases
112116
if get_origin(py_type) is not Annotated:
113117
return [], py_type

tests/test_plain_class.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# specific language governing permissions and limitations under the License.
1111

1212
import re
13-
from typing import Annotated
13+
from typing import Annotated, Final
1414

1515
import pytest
1616

@@ -143,3 +143,22 @@ class PyType:
143143

144144
expected = {"fields": [{"name": "details", "type": "string"}], "name": "PyType", "type": "record"}
145145
assert_schema(PyType, expected)
146+
147+
148+
def test_typing_final():
149+
150+
class PyType:
151+
var: Final[str]
152+
field: Final[dict[str, int]]
153+
154+
def __init__(self):
155+
self.var = "Hello World"
156+
self.field = {"John": 123}
157+
158+
expected = {
159+
"fields": [{"name": "var", "type": "string"}, {"name": "field", "type": {"type": "map", "values": "long"}}],
160+
"name": "PyType",
161+
"type": "record",
162+
}
163+
164+
assert_schema(PyType, expected)

0 commit comments

Comments
 (0)