Skip to content

Python error in generated models.py code #88

Description

@mikemccabe

Hi, I'm pretty sure this is a regression, as it started for me recently after I installed the latest version of this library.

The generated models.py for my resource includes this method:

@dataclass
class ResourceModel(BaseModel):
    SubuserName: Optional[str]
    Domain: Optional[str]
    Subdomain: Optional[str]
    Id: Optional[str]
    Valid: Optional[str]
    Dns: Optional[Sequence["_DnsEntry"]]

    @classmethod
    def _deserialize(
        cls: Type["_ResourceModel"],
        json_data: Optional[Mapping[str, Any]],
    ) -> Optional["_ResourceModel"]:
        if not json_data:
            return None
        dataclasses = {n: o for n, o in getmembers(sys.modules[__name__]) if isclass(o)}
        recast_object(cls, json_data, dataclasses)
        return cls(
            SubuserName=json_data.get("SubuserName"),
            Domain=json_data.get("Domain"),
            Subdomain=json_data.get("Subdomain"),
            Id=json_data.get("Id"),
            Valid=json_data.get("Valid"),
            Dns=deserialize_list(json_data.get("Dns"), Dns),
        )

The issue here is that the last reference to Dns refers to a name that's not in scope (as I believe it's an instance variable?) leading to a NameError

Traceback (most recent call last):
  File "/var/task/cloudformation_cli_python_lib/resource.py", line 140, in _parse_test_requ$
    ).to_modelled(self._model_cls)
  File "/var/task/cloudformation_cli_python_lib/utils.py", line 123, in to_modelled
    desiredResourceState=model_cls._deserialize(self.desiredResourceState),
  File "/var/task/nextdoor_sendgrid_domain/models.py", line 66, in _deserialize
    Dns=deserialize_list(json_data.get("Dns"), Dns),
NameError: name 'Dns' is not defined

I was able to patch this and get my tests to pass by replacing Dns with DnsEntry(None, None, None, None, None). (Just using None didn't work, as it led to an error on a missing _deserialize method.)

Obviously this won't work for the long term, as models.py is generated.

Activity

  1. mikemccabe commented on Apr 19, 2020

    @mikemccabe
    Author

    Here's my resource definition.

    {
        "typeName": "Nextdoor::SendGrid::Domain",
        "description": "Wrapper for /whitelabel/domains, see https://sendgrid.com/docs/User_Guide/Settings/Whitelabel/index.html",
        "sourceUrl": "https://github.com/aws-cloudformation/aws-cloudformation-rpdk.git",
        "definitions": {
            "DnsEntry": {
                "type": "object",
                "properties": {
                    "Name": {
                        "description": "Name for needed dns entry, e.g. 'mail_cname', 'dkim1'",
                        "type": "string"
                    },
                    "Valid": {
                        "description": "Has entry been validated?",
                        "type": "string"
                    },
                    "Type": {
                        "description": "cname, etc",
                        "type": "string",
                        "pattern": "^(cname|mx|txt)$"
                    },
                    "Host": {
                        "description": "dns record host",
                        "type": "string"
                    },
                    "Data": {
                        "description": "dns record content",
                        "type": "string"
                    }
                }
            }
        },
    
        "properties": {
            "SubuserName": {
                "description": "Subuser Name",
                "type": "string",
                "pattern": "^test-mikemccabe-test$",
                "$comment": "^(is|rs|hs|ms)-[a-z]{2}-[a-z]{8}$"
            },
            "Domain": {
                "description": "Domain",
                "type": "string",
                "pattern": "^[a-z]\\.[a-z]+$"
            },
            "Subdomain": {
                "description": "Subdomain",
                "type": "string",
                "pattern": "^[a-z]+$"
            },
            "Id": {
                "type": "string"
            },
            "Valid": {
                "type": "string"
            },
            "Dns": {
                "type": "array",
                "items": {
                    "$ref": "#/definitions/DnsEntry"
                }
            }
        },
        "required": [
            "SubuserName",
            "Domain",
            "Subdomain"
        ],
        "readOnlyProperties": [
            "/properties/Id",
            "/properties/Valid",
            "/properties/Dns"
        ],
        "primaryIdentifier": [
            "/properties/Id"
        ],
        "handlers": {
            "create": {
                "permissions": [""]
            },
            "read": {
                "permissions": [""]
            },
            "delete": {
                "permissions": [""]
            },
            "list": {
                "permissions": [""]
            }
        },
        "additionalProperties": false
    }
    
  2. LaikaN57 commented on Jul 16, 2020

    @LaikaN57

    @mikemccabe Looks like this might have been duplicated in #96 and fixed in #98.

  3. LaikaN57 commented on Jul 16, 2020

    @LaikaN57

    @ammokhov Looks like a duplicate. I think we can close this issue.

  4. ammokhov commented on Jul 16, 2020

    @ammokhov
    Contributor

    @ammokhov Looks like a duplicate. I think we can close this issue.

    good catch! closing as been fixed in #98

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions