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

Can't load system if two files have the same name #642

Closed
guillett opened this issue Apr 12, 2018 · 6 comments
Closed

Can't load system if two files have the same name #642

guillett opened this issue Apr 12, 2018 · 6 comments
Assignees
Labels
kind:fix Bugs are defects and failure demand.

Comments

@guillett
Copy link
Member

guillett commented Apr 12, 2018

Hi there!

I really enjoy OpenFisca, but I recently encountered an issue.

Here is what I did:

Last week, I was in France countryside and I pair-programmed to code a local benefit.

I did not manage to show our product with all the extensions activated because I had an exception when loading the French tax benefit system.

To demo the issue, I created guillett/test_openfisca_extensions.

I managed to investigate the issue today and I think I found the culprit line.

openfisca_core/taxbenefitsystems.py:152

    def add_variables_from_file(self, file_path):
        """
        Adds all OpenFisca variables contained in a given file to the tax and benefit system.
        """
        try:
            file_name = path.splitext(path.basename(file_path))[0]
            module_name = '{}_{}'.format(id(self), file_name)  # If two tax and benefit systems load the same module, the second one should not replace the first one. Hence this unique module name.
            module_directory = path.dirname(file_path)
            try:
                module = load_module(module_name, *find_module(file_name, [module_directory]))
            except NameError as e:
                logging.error(str(e) + ": if this code used to work, this error might be due to a major change in OpenFisca-Core. Checkout the changelog to learn more: <https://github.com/openfisca/openfisca-core/blob/master/CHANGELOG.md>")
                raise
            potential_variables = [getattr(module, item) for item in dir(module) if not item.startswith('__')]
            for pot_variable in potential_variables:
                # We only want to get the module classes defined in this module (not imported)
                if isclass(pot_variable) and \
                        issubclass(pot_variable, Variable) and \
                        pot_variable.__module__.endswith(module_name): #   <----------- THIS LINE HERE
                    self.add_variable(pot_variable)
        except:
            log.error(u'Unable to load OpenFisca variables from file "{}"'.format(file_path))
raise

pot_variable.__module__.endswith(module_name)

If two modules have the same names (ie. two files) that will fail.

I managed to replicate this issue with less complexity (ie. only with the country-template package).

Context

I identify more as a:

  • Developer (I create tools that use the existing OpenFisca code).
@fpagnoux
Copy link
Member

Thanks for reporting the issue @guillett !

Will try to have a look at it this week.

@fpagnoux
Copy link
Member

More likely next week actually, sorry about that.

@bonjourmauko
Copy link
Member

@fpagnoux you did have a similar issue loading different tax-benefit systems, didn't you? Can't find the commit, though.

@bonjourmauko bonjourmauko added the kind:fix Bugs are defects and failure demand. label Apr 16, 2018
@guillett
Copy link
Member Author

Thanks everyone!

Note: I just dumped some of my issues here so that I do not forget them.

@fpagnoux
Copy link
Member

fpagnoux commented Apr 16, 2018

@fpagnoux you did have a similar issue loading different tax-benefit systems, didn't you? Can't find the commit, though.

You're probably thinking of f7475d031f7bd669c16a0acf30bf1e232c2876a4 . I had a weird issue with loading twice the same tax and benefit system, but it was not exactly the same issue.

@bonjourmauko
Copy link
Member

Thanks everyone!

Note: I just dumped some of my issues here so that I do not forget them.

Please do! There's a high chance your issues aren't only yours. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:fix Bugs are defects and failure demand.
Projects
None yet
Development

No branches or pull requests

3 participants