From 36ba8731e626372f16ee64c382d81696aec12f1c Mon Sep 17 00:00:00 2001 From: Bu5hm4nn Date: Mon, 30 Mar 2026 00:02:54 +0200 Subject: [PATCH] fix(types): core calculations mypy errors - isinstance checks, OptionType cast --- app/core/calculations.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/app/core/calculations.py b/app/core/calculations.py index bc6b772..da1286b 100644 --- a/app/core/calculations.py +++ b/app/core/calculations.py @@ -2,11 +2,13 @@ from __future__ import annotations from collections.abc import Iterable, Mapping from datetime import date, datetime +from typing import cast from app.core.pricing.black_scholes import ( DEFAULT_RISK_FREE_RATE, DEFAULT_VOLATILITY, BlackScholesInputs, + OptionType, black_scholes_price_and_greeks, ) from app.models.option import OptionContract @@ -124,10 +126,8 @@ def option_row_greeks( if underlying_price <= 0: return dict(_ZERO_GREEKS) - try: - strike = float(row.get("strike", 0.0)) - except (TypeError, ValueError): - return dict(_ZERO_GREEKS) + strike_raw = row.get("strike", 0.0) + strike = float(strike_raw) if isinstance(strike_raw, (int, float)) else 0.0 if strike <= 0: return dict(_ZERO_GREEKS) @@ -149,12 +149,11 @@ def option_row_greeks( if days_to_expiry <= 0: return dict(_ZERO_GREEKS) - try: - implied_volatility = float(row.get("impliedVolatility", 0.0) or 0.0) - except (TypeError, ValueError): - implied_volatility = 0.0 + iv_raw = row.get("impliedVolatility", 0.0) or 0.0 + implied_volatility = float(iv_raw) if isinstance(iv_raw, (int, float)) else 0.0 volatility = implied_volatility if implied_volatility > 0 else DEFAULT_VOLATILITY + option_type_typed: OptionType = cast(OptionType, option_type) try: pricing = black_scholes_price_and_greeks( BlackScholesInputs( @@ -163,7 +162,7 @@ def option_row_greeks( time_to_expiry=days_to_expiry / 365.0, risk_free_rate=risk_free_rate, volatility=volatility, - option_type=option_type, + option_type=option_type_typed, valuation_date=valuation, ) )