-
-
Notifications
You must be signed in to change notification settings - Fork 876
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 subscriptable support for PropertyHolder #1102
Conversation
This would be a simple, nice to have feature for dealing with custom fields 😃 Alternatively, I will just keep patching it in ```python from jira import resources as jira_resources def _jira_issue_property_holder_get_item(cls, item): if item.startswith("_"): raise KeyError(item) else: return getattr(cls, item) jira_resources.PropertyHolder._getitem__ = _jira_issue_property_holder_get_item ```
sounds cool... a solution to a problem we all encountered, i cant say if this is an ideal solution on the other hand :-s seems to be open for much subjectiveness ... I would prefer some other people to have a look before approving. in any case, please extend the tests. We wont merge anything without a proper test. Also you have a mypy error |
I will try to have a look next week at work, to see if I can create some proper test for review, pointers are welcome 😉 |
@adehad your 2 cents on this would be appreciated. As said it's a possible solution to a problem many people are having. (Including me) First look up the custom field number then get the atr since you can only get the string from JIRA().fields()... |
I think auto-completion/suggestions won't be possible just yet with how mypy and typing work. I'd be gladly proved wrong, but I think let's not worry about that for now.
So we currently have Lines 497 to 501 in f26ea39
Which is used in Lines 2722 to 2727 in f26ea39
So perhaps we could look into creating a function that goes from field name to id, although I have a feeling that there may be cases where custom fields with the same name can exist - so I personally wouldn't want to rely on a solution like that in my work, but no harm documenting this limitation in such a function.
Apart from What I've done in my work is actually something like: some_custom_field = "customfield_15623"
value = issue.raw["fields"][some_custom_field] From my perspective the most safe way to implement this would be so add directly to the def get_field(self, field_name):
return self.raw["fields"].get(field_name) |
I see what you @adehad with a method being a more preferred way to go, but I don't like that it will return
Regarding auto-complete, I also don't know how one could do this, but I did get an idea there could help a little (inspired by @adehad preference suggestion) def get_field(self, field_name: str) -> Any:
...
def get_field_int(self, field_name: str) -> int:
...
def get_field_str(self, field_name: str) -> str:
...
def get_field_dict(self, field_name: str) -> Dict[str, Any]:
...
def get_field_list(self, field_name: str) -> List[Any]:
... I just don't know if this is something you wanted, or if it becomes too complete, but my logic is that if you want to get/use a customfield, you already know what type it is, so this way you would get (some) auto-completion for whatever variable is returned |
Yep I was just copying the behaviour of
I think for now we can encourage a use like this for the type suggestion: def get_field(self, field_name: str) -> Any:
...
# user to specify specific types
my_int: int = issue.get_field("customfield_15623")
my_str: str = issue.get_field("customfield_15623") I was thinking @studioj might have been referring to autocompletion in the editor, e.g. where currently we have listed some fields that should exist in the |
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.
Some works needs to be done on the tests and documentation, but I think this approach is shaping up well
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.
tests still need some rework
just forget your changes in all the other tests as @adehad mentions
just add two testmethods
one where you raise an arrtibute error on purpose
self.assertRaises(AttributeError, get_field, "_name_starts_with_underscore")
and another one which follows the happy path
Hey, any updates on this? GetItemMixin class GetItemMixin:
def __getitem__(self, item: str):
try:
return getattr(self, item)
except AttributeError:
raise ValueError(f"Field {item} does not exist in fields, is it included in the fields parameter?")
def __setitem__(self, key: str, value: Any):
if key in self and self[key] is not None and value is not None:
assert isinstance(value, type(self[key])) # some help with setting the correct value kind
setattr(self, key, value)
def __contains__(self, item):
try:
getattr(self, item)
return True
except AttributeError:
return False |
I ended up getting quite busy until new-year stuff is starting to calm down again. I may be able to have a look at it again because I also need it myself at work. Cannot keep patching this feature in, in multiple scripts 😅 |
I have updated the patch. It should now contain the documentation you suggested, I have also, created an unittest there tests success and 2 different exceptions. If there are scenarios that I overlooked, plz tell me and I will get them added 🙂 |
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.
Awesome, looking much better @dvaerum.
Just a minor point regarding the naming convention of variables.
There is also a non-blocking comment, so feel free to ignore if you feel strongly about it.
Added fix for name convention Co-authored-by: Adel Haddad <26027314+adehad@users.noreply.github.com>
Change unittest to "context manager style" Co-authored-by: Adel Haddad <26027314+adehad@users.noreply.github.com>
Fixed spelling mistage Co-authored-by: Adel Haddad <26027314+adehad@users.noreply.github.com>
Done, I have added all your suggestions (and comments to them), but it doesn't look like it will run the tests :/ |
Changed the rest of the variable names from `fieldName` to `field_name` Co-authored-by: Adel Haddad <26027314+adehad@users.noreply.github.com>
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.
Awesome, thanks for getting this through!
Thanks for working with me on this :D |
This would be a simple, nice to have feature for dealing with custom fields 😃
It makes it possible to do
instead of
Alternatively, I will just keep patching it in