amazon-web-servicesaws-cdkpython-3.12

AWS CDK error TypeError: Subscripted generics cannot be used with class and instance checks


I have a infrastructure file to deploy the resources to AWS using python's aws-cdk.

Here is my psuedo code

cpu_utilization_alarm = cloudwatch.Alarm(self, 'CPUUtilizationAlarm',
   # other config
)

cpu_utilization_alarm.add_alarm_action(cloudwatch_actions.SnsAction(cloudwatch_alert_sns_topic))

Upon running this deployment aws-cdk from typeguard throws error as: TypeError: Subscripted generics cannot be used with class and instance checks, and is always failing at cpu_utilization_alarm.add_alarm_action

I tried the following things until now:

  1. Comment the line to debug, the code runs without any issue
  2. Make the parameter as tuple like cpu_utilization_alarm.add_alarm_action(cloudwatch_actions.SnsAction(cloudwatch_alert_sns_topic),)
  3. Tried to use @typeguard_ignore
  4. Initialize cloudwatch_actions.SnsAction(cloudwatch_alert_sns_topic) to a variable then pass as paramter.

server (and local) configuration:

Iam not sure, what am I missing, is it the configuration or typing or typeguard prevent me. Let me know if anymore information is required.

Here is the traceback for error

    cpu_utilization_alarm.add_alarm_action(cloudwatch_actions.SnsAction(cloudwatch_alert_sns_topic))
  File "python3.12/site-packages/aws_cdk/aws_cloudwatch/__init__.py", line 14809, in add_alarm_action
    check_type(argname="argument actions", value=actions, expected_type=typing.Tuple[type_hints["actions"], ...]) # pyright: ignore [reportGeneralTypeIssues]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "python3.12/site-packages/typeguard/__init__.py", line 759, in check_type
    checker_func(argname, value, expected_type, memo)
  File "python3.12/site-packages/typeguard/__init__.py", line 497, in check_tuple
    if not isinstance(value, expected_type):
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/typing.py", line 1213, in __instancecheck__
    return self.__subclasscheck__(type(obj))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/typing.py", line 1216, in __subclasscheck__
    raise TypeError("Subscripted generics cannot be used with"
TypeError: Subscripted generics cannot be used with class and instance checks

Solution

  • I figured out the solution, I need to add export PYTHONOPTIMIZE=1 to enable -O flag which disables __debug__ and bypass the pylint checks