
    i                    \    d Z ddlmZ ddlZddlZ ej
                  e      ZdZddZ	ddZ
y)	u  
APEX V18 — runtime monkey-patches for the project-x-py SDK.

These patches survive `pip install -r requirements.txt` and venv
recreation because they're applied at import time from our own code,
not by editing the installed package.

Each patch is idempotent (re-applying is a no-op via a sentinel
attribute on the patched class).

Patches:
  - Position.__init__ filter: silently ignore unknown keyword args.
    Reason: TopstepX added `contractDisplayName` (2026-05) to the
    /api/Position response and may add more fields. The original
    @dataclass __init__ rejects unknown kwargs, breaking
    positions_get(). The wrapper filters kwargs to declared fields
    before delegating, so future API additions are absorbed silently.
    )annotationsN_apex_v18_patchedc                 x   	 ddl m}  t        | t        d      ry| j                  t        j                  |       D ch c]  }|j                   c}fd}|| _        t        | t        d       t        j                  dt                     y# t        $ r t        j	                  d       Y yw xY wc c}w )zWrap project_x_py.models.Position.__init__ to ignore unknown
    kwargs. Returns True if patch applied, False if already patched
    or SDK absent.r   )Positionz/SDK patch: project_x_py not installed; skippingFc                z    |j                         D ci c]  \  }}|v s|| }}} | g|i | y c c}}w )N)items)selfargskwargskvfiltered_known_fields
_orig_inits         */home/work/apex_v16/broker/_sdk_patches.py_patched_initz+_patch_position_init.<locals>._patched_init.   sC    %+\\^JTQqM7IAqDJJ4+$+(+ Ks   77TzNSDK patch applied: Position.__init__ ignores unknown kwargs (known fields: %s))project_x_py.modelsr   ImportErrorloggerdebuggetattr_PATCH_SENTINEL__init__dataclassesfieldsnamesetattrinfosorted)r   fr   r   r   s      @@r   _patch_position_initr!      s    0
 x%0""J%0%7%7%ABQVVBM, &HHot,
KK	$]3 )  FG Cs   B B7B43B4c                     t                y)zoApply all APEX-side monkey-patches to project_x_py. Safe to
    call multiple times (each patch is idempotent).N)r!        r   apply_sdk_patchesr%   ;   s
     r$   )returnbool)r&   None)__doc__
__future__r   r   logging	getLogger__name__r   r   r!   r%   r#   r$   r   <module>r.      s6   & #  			8	$%:r$   