Skip to content
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 a /entities endpoint to the API #714

Merged
merged 12 commits into from
Sep 4, 2018
Merged

Add a /entities endpoint to the API #714

merged 12 commits into from
Sep 4, 2018

Conversation

Anna-Livia
Copy link
Contributor

@Anna-Livia Anna-Livia commented Aug 23, 2018

Fixes #561

New features

  • Get information on a country package's entities, and their roles
  • Introduce the /entities endpoint for the Web API
{
            "description": "[...]",
            "plural": "households",
            "roles": {
                "parent": {
                    "description": "The one or two adults in charge of the household.",
                    "mandatory": "true",
                    "max": 2,
                    "plural": "parents"
                    },
                "child": {
                    "description": "Other individuals living in the household.",
                    "mandatory": "false",
                    "plural": "children"
                    }
                }
            }

@Anna-Livia Anna-Livia requested review from fpagnoux and sandcha August 23, 2018 16:28
Copy link
Member

@fpagnoux fpagnoux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👍 !

We're finally one step closer from documenting the entities in the legislation explorer 🎉

CHANGELOG.md Outdated
- Introduce the `/entities` endpoint for the Web API
```json
{
"description": "[...]",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we make up a simple description? Examples will real values are always easier to grasp 🙂

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, in my previous comment I thought description was the label, now I understand better why you didn't want to write the full Household doc.
But I still think a one or two-sentence max doc would hep :)


entity_formated = {
'plural': entity.plural,
'description': to_unicode(entity.doc)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#561 mentioned a documentation key containing the docstring.
If we are to have a description key, it should probably contain entity.label

'description': to_unicode(entity.doc)
}
if hasattr(entity, 'roles'):
entity_formated['roles'] = \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid \ for line continuation: you can rely on implicit continuation, as you've done everywhere else 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, pycharm outsmarted me. ;)

'plural': entity.plural,
'description': to_unicode(entity.doc)
}
if hasattr(entity, 'roles'):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually use:

if entity.is_person:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't a group entity not have roles ?
it seems to me that if entity.is_person: would give me all group entities whether they have rolesor not 🤔

Copy link
Member

@fpagnoux fpagnoux Aug 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point.

We have never used a group entity without roles, and right now it would trigger an ugly error, but you're right, there could be use cases.

However, even if we had a group entity without roles, it should still have an attribute roles that would be [], so the code further down would still work. In general, using hasattr is pretty rare unless you are doing relatively tricky stuff.

if role.max:
role_formated['max'] = role.max
if role.subroles:
role_formated['max'] = len(role.subroles)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

if role.subroles:
role_formated['max'] = len(role.subroles)

role_formated['mandatory'] = True if role_formated.get('max') else False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, there is no good reason to assume than a role with a max is a mandatory role.

Right now, we don't handle mandatory role in OpenFisca Core. I see two solutions to deal with that:

  1. Always set role_formated['mandatory'] to False (the Web API would be "anticipating" the feature)
  2. Open another PR in Core introducing this mandatory attribute in Python

1 allows for a quick delivery, but leads to a relatively inconsistent state of the codebase.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I implemented 1 and opened #721 to follow 2

@@ -16,13 +16,18 @@ tags:
- name: "Parameters"
description: "A parameter is a numeric property of the legislation that can evolve over time."
externalDocs:
description: "Parameter official documentation"
description: "Official Parameter documentation"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not we sure what we mean by "official". Never seen an fan-based documentation so far, nor an off-brand one ;)


def build_entity(entity):

entity_formated = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

formatted_entity ?


def test_response_data():
entities = json.loads(entities_response.data.decode('utf-8'))
test_description = '''
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy-pasting the description makes the test really fragile. A minor edition of the doc would break it.
I'd recommend importing Household from the country template and getting the doc from Household.doc

'roles': {
'parent': {
'description': 'The one or two adults in charge of the household.',
'mandatory': True,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably stick to False unless we actually handle mandatory roles.

Copy link
Member

@fpagnoux fpagnoux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, there is a couple of things I missed during my first review, but we are almost there :) !

additionalProperties:
type: "object"
properties:
definition:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually don't think there is a definition property for our entities object. We should also document the following properties:

  • description
  • mandatory
  • max
  • plural
  • documentation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and roles ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, forgot that one 😓

if role.subroles:
formatted_role['max'] = len(role.subroles)

formatted_role['mandatory'] = False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The more I think about it, the least confortable I feel about this mandatory property.

For instance, are we sure the following

max: 2
mandatory: true

is clearer than

max:2
min:1

?

I'd be in favor of removing that property and postponing this discussion to #721

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I totally agree

formatted_entity = {
'plural': entity.plural,
'description': to_unicode(entity.label),
'documentation': to_unicode(entity.doc)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

documentation seems to often start and end with \n, which is not really relevant. We should probably clean these :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a way but it is not very elegant.... I'm looking forward to your feedback on it :)

@Anna-Livia Anna-Livia requested a review from fpagnoux September 4, 2018 08:34
split_doc = to_unicode(entity.doc).split("\n")
formatted_start_doc = split_doc[1:] if split_doc[0] == "" else split_doc
formatted_end_doc = formatted_start_doc[:-1] if formatted_start_doc[-1] == "" else formatted_start_doc
formatted_doc = "\n".join(formatted_end_doc)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can just use formatted_doc = to_unicode(entity.doc.strip())

strip removes all trailing white spaces, and \n is considered as a particular white space 🎉

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... 🤦

Copy link
Member

@fpagnoux fpagnoux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

build_entity should be simplified, but GTM otherwise 🙂

@Anna-Livia Anna-Livia merged commit 7aeadae into master Sep 4, 2018
@bonjourmauko bonjourmauko added the kind:feat A feature request, a feature deprecation label Sep 20, 2018
@guillett guillett deleted the entities branch November 15, 2018 07:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:feat A feature request, a feature deprecation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants