anomaly / gallagher

The missing toolkit for extending Gallagher Command Centre, featuring a CLI, SQL interface, TUI and a Python idiomatic SDK
https://anomaly.github.io/gallagher/
MIT License
11 stars 2 forks source link

Refactor (if required, and doesn't break anything) the use of `reserved` keywords as `attribute` names in `pyndatic` models #38

Closed devraj closed 2 months ago

devraj commented 4 months ago

Describe the bug While this works, it's not best practice to use reserved words as attribute names in classes. Consider this class:


class AlarmSummary(
    AppBaseModel,
    HrefMixin,
    IdentityMixin,
):
    time: datetime
    message: str
    source: AlarmSourceSummary
    type: str

type is a reserved word in python and is used as an attribute in the class. IDEs will prompt that this is the wrong thing to:

Screenshot 2024-05-01 at 10 03 06 AM

Pyndatic has support for TypeDict and dataclasses for defining classes, we should look into what is the best way to implement this without losing the developer focus and pythonic nature of the API class.

To Reproduce NA

Expected behavior Implement a proper way

devraj commented 4 months ago

From some basic research the above isn't incorrect unless it causes cyclic issues with validation.

Should we park this until this becomes an issue?

devraj commented 4 months ago

The card for a cardholder has the attribute from which is a valid directive at most levels in python eg:

{
  "href": "https://localhost:8904/api/cardholders/325/cards/97b6a24ard6d4500a9",
  "number": "1",
  "cardSerialNumber": "045A5769713E80",
  "issueLevel": 1,
  "status": {
    "value": "Disabled (manually)",
    "type": "inactive"
  },
  "type": {
    "href": "https://localhost:8904/api/card_types/354",
    "name": "Card type no. 1"
  },
  "invitation": {
    "email": "nick@example.com",
    "mobile": "02123456789",
    "singleFactorOnly": true,
    "status": "sent",
    "href": "https://security.gallagher.cloud/api/invitations/abcd1234defg5678"
  },
  "from": "2017-01-01T00:00:00Z",
  "until": "2017-12-31T11:59:59Z",
  "credentialClass": "mobile",
  "trace": false,
  "lastPrintedOrEncodedTime": "2020-08-10T09:20:50Z",
  "lastPrintedOrEncodedIssueLevel": 1,
  "pin": "153624",
  "visitorContractor": false,
  "ownedBySite": false,
  "credentialId": "reserved",
  "bleFacilityId": "reserved"
}

in light of this we should investigate this further and come up with a solution to not use reserved words at all. Example of using from as a method name not being valid either.

Screenshot 2024-05-13 at 10 52 02 PM

See also #1 for discovery on extras support via pydantic