diff --git a/python_fundamentals/exercises/day18-field-alias.py b/python_fundamentals/exercises/day18-field-alias.py index 10e7657..b383df9 100644 --- a/python_fundamentals/exercises/day18-field-alias.py +++ b/python_fundamentals/exercises/day18-field-alias.py @@ -75,10 +75,13 @@ def change_first_name(self, first_nm: str) -> None: print(exc) try: + # `populate_by_name=True` allows construction with snake_case names; + # the `# type: ignore` comments were unused and flagged by the type checker, + # so remove them. emp2: Employee = Employee( - first_name="swaroop", # type: ignore - last_name="dixit", # type: ignore - monthly_salary=20000, # type: ignore + first_name="swaroop", + last_name="dixit", + monthly_salary=20000, ) print(emp2.model_dump()) except ValidationError as exc: diff --git a/python_fundamentals/exercises/day_24_decimal/product_invoice.py b/python_fundamentals/exercises/day_24_decimal/product_invoice.py index 9b69c88..07bcf8e 100644 --- a/python_fundamentals/exercises/day_24_decimal/product_invoice.py +++ b/python_fundamentals/exercises/day_24_decimal/product_invoice.py @@ -23,14 +23,16 @@ class Invoice(BaseModel): def subtotal_total(self) -> Decimal: total: Decimal = Decimal("0") for product in self.products: - total += product.subtotal + # call the computed_field so static type-checkers see a Decimal result + total += product.subtotal() return total @computed_field def grand_total(self) -> Decimal: - subtotal: Decimal = self.subtotal_total - gst_amount = subtotal * (self.gst_percentage / 100) - return Decimal(subtotal + gst_amount) + subtotal: Decimal = self.subtotal_total() + # divide by Decimal to avoid mixing floats with Decimals + gst_amount = subtotal * (self.gst_percentage / Decimal("100")) + return subtotal + gst_amount p1: Product = Product(name="Sugar", price=Decimal("20.00"), quantity=1) diff --git a/python_fundamentals/exercises/day_25_date_datetime/employee.py b/python_fundamentals/exercises/day_25_date_datetime/employee.py old mode 100755 new mode 100644 index 1ee8d0f..7b09b53 --- a/python_fundamentals/exercises/day_25_date_datetime/employee.py +++ b/python_fundamentals/exercises/day_25_date_datetime/employee.py @@ -20,10 +20,10 @@ class Employee(BaseModel): print(f"Python Representaion of Employee Object: {tejas_dixit.model_dump()}") print(f"Pydantic Representaion of Employee Object: {tejas_dixit.model_dump_json()}") - vignesh_gawali: Employee = Employee( - emp_name="Vignesh Gawali", - joining_date="2024-12-12", # Intentional: testing Pydantic parsing - created_at="0000-12-12 12:12", # Intentional: testing Pydantic parsing + # Use model_validate for string inputs to avoid static type errors and to + # keep intentional parsing tests. + vignesh_gawali = Employee.model_validate( + {"emp_name": "Vignesh Gawali", "joining_date": "2024-12-12", "created_at": "2024-12-12 12:12"} ) print(f"Python Representaion of Employee Object: {vignesh_gawali.model_dump()}") diff --git a/python_fundamentals/exercises/day_26_model_dump_serialization/employee.py b/python_fundamentals/exercises/day_26_model_dump_serialization/employee.py index 284f571..2c25a51 100644 --- a/python_fundamentals/exercises/day_26_model_dump_serialization/employee.py +++ b/python_fundamentals/exercises/day_26_model_dump_serialization/employee.py @@ -42,12 +42,9 @@ def format_emp() -> None: format_emp() - another_emp: Employee = Employee( - emp_name="Tejas Dixit", - joining_date="2024-12-29", - # Intentional: Pydantic will conert it into date format - created_at="2024-12-28 12:12", - # Intentional: Pydantic model will convert it into datetime format + # Use model_validate to pass string inputs (so static checkers don't flag arg-type). + another_emp = Employee.model_validate( + {"emp_name": "Tejas Dixit", "joining_date": "2024-12-29", "created_at": "2024-12-28 12:12"} ) print( @@ -56,6 +53,5 @@ def format_emp() -> None: print(f"ADMIN View: {another_emp.model_dump(exclude={'joining_date'})}") - except (ValueError, ValidationError) as exc: print(exc) diff --git a/python_fundamentals/exercises/pydantic-extra.py b/python_fundamentals/exercises/pydantic-extra.py index 95d5eba..a7c5551 100644 --- a/python_fundamentals/exercises/pydantic-extra.py +++ b/python_fundamentals/exercises/pydantic-extra.py @@ -39,10 +39,10 @@ class EventLog(BaseModel): print("=" * 60) try: - event = EventLog( - event_id=0, - event_type="Error", - event_description="ValidationError", + # Use model_validate when passing extra fields via a mapping so static type + # checkers don't complain about unexpected keyword arguments. + event = EventLog.model_validate( + {"event_id": 0, "event_type": "Error", "event_description": "ValidationError"} ) print(event.model_dump())