-
-
Notifications
You must be signed in to change notification settings - Fork 318
Preserve RFC 7265 unknown property values verbatim #1450
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
Open
lcampanella98
wants to merge
5
commits into
collective:main
Choose a base branch
from
lcampanella98:fix-1445-unknown-verbatim
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+377
−16
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e7c1c63
Preserve RFC 7265 unknown property values verbatim
lcampanella98 e49b699
Apply docstring suggestions from code review
lcampanella98 92cce9a
Address review: merge parts(), tidy vUnknown docstrings
lcampanella98 3a77e3d
Add vText doctest example
lcampanella98 92dbf5b
Merge branch 'main' into fix-1445-unknown-verbatim
stevepiercy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Values of unrecognized properties and ``X-`` properties without a ``VALUE`` parameter were altered by escaping when parsed, serialized, or converted to and from jCal, so they did not round-trip unchanged as :rfc:`7265` specifies. These values are now preserved verbatim. Additionally, the ``PROXIMITY`` property (:rfc:`9074`) was treated as an unknown value type instead of ``TEXT`` and is now recognized correctly. AI disclosure: I used Claude Code (Anthropic's Claude Opus) to draft and refine this change and its tests; I reviewed the output and validated the change locally. @lcampanella98 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,132 @@ | ||
| """UNKNOWN values from :rfc:`7265`.""" | ||
|
|
||
| from typing import ClassVar | ||
| from typing import Any, ClassVar | ||
|
|
||
| from icalendar.compatibility import Self | ||
| from icalendar.prop.text import vText | ||
| from icalendar.error import JCalParsingError | ||
| from icalendar.parser import Parameters | ||
| from icalendar.parser_tools import DEFAULT_ENCODING, ICAL_TYPE, to_unicode | ||
|
|
||
|
|
||
| class vUnknown(vText): | ||
| """This is text but the VALUE parameter is unknown. | ||
| class vUnknown(str): | ||
| """A property value of the :rfc:`7265#section-5` reserved UNKNOWN value data type. | ||
|
|
||
| Since :rfc:`7265`, it is important to record if values are unknown. | ||
| For :rfc:`5545`, we could just assume TEXT. | ||
| .. versionchanged:: 7.2.0 | ||
|
|
||
| Previously ``vUnknown`` inherited from ``vText``, which unescapes values. | ||
| Now ``vUnknown`` doesn't unescape its values, which is the correct behavior. | ||
|
|
||
| Unlike :class:`~icalendar.prop.text.vText`, the value is preserved verbatim | ||
| when imported from and exported to iCalendar data, without :rfc:`5545` escaping | ||
| or unescaping. When the value type of an unrecognized property is not known, | ||
| then no escaping rules can be applied, and the value must be preserved as is | ||
| round-trip. | ||
|
|
||
| See also: | ||
|
|
||
| :rfc:`7265#section-5.1` | ||
| """ | ||
|
|
||
| default_value: ClassVar[str] = "UNKNOWN" | ||
| params: Parameters | ||
| __slots__ = ("encoding", "params") | ||
|
|
||
| def __new__( | ||
| cls, | ||
| value: ICAL_TYPE, | ||
| encoding: str = DEFAULT_ENCODING, | ||
| /, | ||
| params: dict[str, Any] | None = None, | ||
| ) -> Self: | ||
| value = to_unicode(value, encoding=encoding) | ||
| self = super().__new__(cls, value) | ||
| self.encoding = encoding | ||
| self.params = Parameters(params) | ||
| return self | ||
|
|
||
| def __repr__(self) -> str: | ||
| return f"vUnknown({self.to_ical()!r})" | ||
|
|
||
| def to_ical(self) -> bytes: | ||
| r"""Return the value verbatim, without :rfc:`5545` escaping. | ||
|
|
||
| This method's implementation is different from that in | ||
| :class:`~icalendar.prop.text.vText`, whose | ||
| :meth:`~icalendar.prop.text.vText.to_ical` method escapes ``;``, ``,``, | ||
| ``\``, and newlines. | ||
|
|
||
| Example: | ||
|
|
||
| The semicolon is kept verbatim for UNKNOWN, unlike a TEXT value | ||
| which would escape it as ``\\;``. | ||
|
|
||
| .. code-block:: pycon | ||
|
|
||
| >>> from icalendar.prop import vText, vUnknown | ||
| >>> vUnknown("a;b").to_ical() | ||
| b'a;b' | ||
|
lcampanella98 marked this conversation as resolved.
|
||
| >>> vText("a;b").to_ical() | ||
| b'a\\;b' | ||
|
|
||
| See also: | ||
|
|
||
| :rfc:`7265#section-5.2` | ||
|
|
||
| """ | ||
| return self.encode(self.encoding) | ||
|
|
||
| @classmethod | ||
| def from_ical(cls, ical: ICAL_TYPE) -> Self: | ||
| """Take the value verbatim, without unescaping.""" | ||
| return cls(ical) | ||
|
|
||
| @property | ||
| def ical_value(self) -> str: | ||
| """The string value of the property.""" | ||
| return str(self) | ||
|
|
||
| from icalendar.param import ALTREP, GAP, LANGUAGE, RELTYPE, VALUE | ||
|
stevepiercy marked this conversation as resolved.
|
||
|
|
||
| def to_jcal(self, name: str) -> list: | ||
| """The jCal representation of this property, according to :rfc:`7265#section-5.1`. | ||
|
|
||
| If the property doesn't include a VALUE property parameter and its value | ||
| type is not known, then its value type is set to ``"unknown"``. Else the | ||
| property's value type is converted to lowercase. | ||
|
|
||
| The property's value is the unprocessed value text, aside from standard | ||
| JSON string escaping. | ||
| """ | ||
| return [name, self.params.to_jcal(), self.VALUE.lower(), str(self)] | ||
|
|
||
| @classmethod | ||
| def examples(cls) -> list[Self]: | ||
| """Examples of vUnknown.""" | ||
| return [vUnknown("Some property text.")] | ||
| return [cls("Some property text.")] | ||
|
|
||
| @classmethod | ||
| def from_jcal(cls, jcal_property: list) -> Self: | ||
| """Parse jCal from :rfc:`7265`, taking the value verbatim. | ||
|
|
||
| from icalendar.param import VALUE | ||
| Parameters: | ||
| jcal_property: The jCal property to parse. | ||
|
|
||
| Raises: | ||
| ~error.JCalParsingError: If the provided jCal is invalid. | ||
| """ | ||
| JCalParsingError.validate_property(jcal_property, cls) | ||
| string = jcal_property[3] | ||
| JCalParsingError.validate_value_type(string, str, cls, 3) | ||
| return cls( | ||
| string, | ||
| params=Parameters.from_jcal_property(jcal_property), | ||
| ) | ||
|
|
||
| @classmethod | ||
| def parse_jcal_value(cls, jcal_value: Any) -> Self: | ||
| """Parse a jCal value into a vUnknown.""" | ||
| JCalParsingError.validate_value_type(jcal_value, (str, int, float), cls) | ||
| return cls(str(jcal_value)) | ||
|
|
||
|
|
||
| __all__ = ["vUnknown"] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.