-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add cloud event to core #16800
Add cloud event to core #16800
Conversation
from datetime import timezone | ||
TZ_UTC = timezone.utc # type: ignore | ||
except ImportError: | ||
class UTC(tzinfo): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am thinking if we can leverage/refactor the code https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/core/azure-core/azure/core/pipeline/policies/_utils.py#L30
Double check: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/core/azure-core/azure/core/pipeline/policies/_utils.py imports requests which is heavy. Do you really want it? That's the reason I said maybe we want to refactor the code. :) (this is not blocker. just want to make sure we take it into account) |
if data and data_base64: | ||
raise ValueError("Invalid input. Only one of data and data_base64 must be present.") | ||
return cls( | ||
id=event.pop("id", None), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This indentation looks odd?
type=event.pop("type", None), | ||
specversion=event.pop("specversion", None), | ||
data=data or b64decode(data_base64), | ||
time=event.pop("time", None), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if time is datetime, need deserializaiton here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it'd be a string
/azp run python - autorest - pr |
Azure Pipelines successfully started running 1 pipeline(s). |
This pull request is protected by Check Enforcer. What is Check Enforcer?Check Enforcer helps ensure all pull requests are covered by at least one check-run (typically an Azure Pipeline). When all check-runs associated with this pull request pass then Check Enforcer itself will pass. Why am I getting this message?You are getting this message because Check Enforcer did not detect any check-runs being associated with this pull request within five minutes. This may indicate that your pull request is not covered by any pipelines and so Check Enforcer is correctly blocking the pull request being merged. What should I do now?If the check-enforcer check-run is not passing and all other check-runs associated with this PR are passing (excluding license-cla) then you could try telling Check Enforcer to evaluate your pull request again. You can do this by adding a comment to this pull request as follows: What if I am onboarding a new service?Often, new services do not have validation pipelines associated with them, in order to bootstrap pipelines for a new service, you can issue the following command as a pull request comment: |
:type event: dict | ||
:rtype: CloudEvent | ||
""" | ||
data = event.pop("data", None) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we want to mutate the argument passed in. I would be a bit surprised to notice that my dict had lost (all of its) keys when the method returned.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed this
source=event.pop("source", None), | ||
type=event.pop("type", None), | ||
specversion=event.pop("specversion", None), | ||
data=data or b64decode(data_base64), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if data
is ''
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed this a lil bit - data '' is accepted
"Extension attributes should be lower cased and alphanumeric." | ||
) | ||
except KeyError: | ||
self.extensions = cast(Dict, None) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This cast does not look correct - it looks like self.extensions should be Optional[Dict]
, not Dict
?
"Invalid input. Only one of data and data_base64 must be present." | ||
) | ||
if 'data' not in event and 'data_base64' not in event: | ||
kwargs.setdefault("data", None) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you using setdefault
here? It is generally used when you get a partially filled out dict
and you don't want to overwrite existing values. But you are defining kwargs
in the method (i.e. it is not a parameter passed in to the method).
Please rename, and use direct assignment (e.g. `eventargs['data'] = None)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...also, do you really need to set "data"
to None
here? The dict
passed in didn't have it, so you should be able to create the CloudEvent instance without passing in the keyword argument, right?
self.id = kwargs.pop("id", str(uuid.uuid4())) # type: str | ||
self.time = kwargs.pop("time", datetime.now(TZ_UTC)) # type: datetime | ||
|
||
_optional_attributes = ["datacontenttype", "dataschema", "subject", "data"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By looping through the attribute names like this, I'm pretty sure that you will not get the right type annotations for the attributes (but I may be wrong). Since it is only 4 names, I'd add them explicitly rather than looping through the names:
self.datacontenttype = kwargs.pop('datacontenttype', None)
This is both easier to read and (less important here) more performant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type annotations should be fixed before we merge (don't fix things by doing incorrect cast
s). Also, not looping through attribute names will provide/allow for typing for those attributes as well.
|
||
return cls( | ||
id=event.get("id"), | ||
source=cast(str, event.get("source")), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there is no "source"
key in event
, you will be (incorrectly) casting None
to str
. It is better to raise a ValueError
here if the either of required keys ("type"
and "source"
) is not in event
or else the error will surface much later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
left it to event["source"]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please address the cast. As a general principle, we need to be careful about casts since doing them incorrectly negates all the value one get from type checking. In which case it is probably better to have them typed as Any rather than making guarantees that one cannot live up to.
…into http_request_json * 'master' of https://github.com/Azure/azure-sdk-for-python: (147 commits) [text analytics] add perf tests (Azure#17060) Add cloud event to core (Azure#16800) [Perf] Small fixes to storage-blob (Azure#17055) [EG] Regenerate Code (Azure#17053) Scrub batch shared keys (Azure#17030) [Tables] Add SAS to tables (Azure#16717) T2 containerservice 2021 03 03 (Azure#17050) Addressing issues with CredScan (Azure#16944) Communication chat preview4 (Azure#16905) (Azure#17037) remove first query section (Azure#17033) [formrecognizer] temp disable sample tests until service bug fixed (Azure#17036) [device update] allow device update pylint failures (Azure#17034) fix build (Azure#17029) update artifact names for ALL packages to align with the actual package name Create azure-iot-nspkg (Azure#17026) [Communication]: SMS 1:N Messages, Custom Tags, and Idempotence (Azure#16836) Fixing credentials to use AAD (Azure#16885) T2 deviceupdate 2021 03 02 (Azure#17016) T2 cosmosdb 2021 02 23 (Azure#16875) T2 datadog 2021 03 02 (Azure#17004) ...
…into add_sample_check * 'master' of https://github.com/Azure/azure-sdk-for-python: (388 commits) [text analytics] add normalized_text (#17074) Renaming with_token identity function (#17066) Adapt to azure core's cloud event (#17063) align perf tests with js (#17069) [Perfstress][Storage] Added FileShare perf tests (#15834) [formrecognizer] Adding custom forms perf test (#16969) Fix LanguageShort typo (#17068) sas creds updates (#17065) [eventgrid] Fix Sample eh (#17064) [Perfstress][Storage] Added Datalake perf tests (#15861) [text analytics] Healthcare n-ary relations (#16997) ServiceBus dict-representation acceptance and kwarg-update functionality (#14807) [text analytics] add perf tests (#17060) Add cloud event to core (#16800) [Perf] Small fixes to storage-blob (#17055) [EG] Regenerate Code (#17053) Scrub batch shared keys (#17030) [Tables] Add SAS to tables (#16717) T2 containerservice 2021 03 03 (#17050) Addressing issues with CredScan (#16944) ...
…into add_query_kwarg * 'master' of https://github.com/Azure/azure-sdk-for-python: (69 commits) march release (Azure#16966) Release of Device Update for IoT Hub SDK for Python. (Azure#17005) Add Get-AllPackageInfoFromRepo (Azure#16947) Track1 package is incorrectly set as track2 (Azure#17075) [text analytics] add normalized_text (Azure#17074) Renaming with_token identity function (Azure#17066) Adapt to azure core's cloud event (Azure#17063) align perf tests with js (Azure#17069) [Perfstress][Storage] Added FileShare perf tests (Azure#15834) [formrecognizer] Adding custom forms perf test (Azure#16969) Fix LanguageShort typo (Azure#17068) sas creds updates (Azure#17065) [eventgrid] Fix Sample eh (Azure#17064) [Perfstress][Storage] Added Datalake perf tests (Azure#15861) [text analytics] Healthcare n-ary relations (Azure#16997) ServiceBus dict-representation acceptance and kwarg-update functionality (Azure#14807) [text analytics] add perf tests (Azure#17060) Add cloud event to core (Azure#16800) [Perf] Small fixes to storage-blob (Azure#17055) [EG] Regenerate Code (Azure#17053) ...
…into add_reprs * 'master' of https://github.com/Azure/azure-sdk-for-python: (71 commits) EG - more docs imrpovement (Azure#17079) [EventHubs] add logging.info to warn the usage of partition key of non-string type (Azure#17057) march release (Azure#16966) Release of Device Update for IoT Hub SDK for Python. (Azure#17005) Add Get-AllPackageInfoFromRepo (Azure#16947) Track1 package is incorrectly set as track2 (Azure#17075) [text analytics] add normalized_text (Azure#17074) Renaming with_token identity function (Azure#17066) Adapt to azure core's cloud event (Azure#17063) align perf tests with js (Azure#17069) [Perfstress][Storage] Added FileShare perf tests (Azure#15834) [formrecognizer] Adding custom forms perf test (Azure#16969) Fix LanguageShort typo (Azure#17068) sas creds updates (Azure#17065) [eventgrid] Fix Sample eh (Azure#17064) [Perfstress][Storage] Added Datalake perf tests (Azure#15861) [text analytics] Healthcare n-ary relations (Azure#16997) ServiceBus dict-representation acceptance and kwarg-update functionality (Azure#14807) [text analytics] add perf tests (Azure#17060) Add cloud event to core (Azure#16800) ...
…into http_response_json * 'master' of https://github.com/Azure/azure-sdk-for-python: (165 commits) EG - more docs imrpovement (Azure#17079) [EventHubs] add logging.info to warn the usage of partition key of non-string type (Azure#17057) march release (Azure#16966) Release of Device Update for IoT Hub SDK for Python. (Azure#17005) Add Get-AllPackageInfoFromRepo (Azure#16947) Track1 package is incorrectly set as track2 (Azure#17075) [text analytics] add normalized_text (Azure#17074) Renaming with_token identity function (Azure#17066) Adapt to azure core's cloud event (Azure#17063) align perf tests with js (Azure#17069) [Perfstress][Storage] Added FileShare perf tests (Azure#15834) [formrecognizer] Adding custom forms perf test (Azure#16969) Fix LanguageShort typo (Azure#17068) sas creds updates (Azure#17065) [eventgrid] Fix Sample eh (Azure#17064) [Perfstress][Storage] Added Datalake perf tests (Azure#15861) [text analytics] Healthcare n-ary relations (Azure#16997) ServiceBus dict-representation acceptance and kwarg-update functionality (Azure#14807) [text analytics] add perf tests (Azure#17060) Add cloud event to core (Azure#16800) ...
Creating a different PR for specifically core related changes - will rebase the other PR for EG related ones
Fixes #16697
Part 1 of #16585