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

Added Python 3 support #31

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Added Python 3 support #31

wants to merge 3 commits into from

Conversation

mottosso
Copy link

Hey Martin,

I've added support for Python 3 and tidied up a few PEP8 issues. Apologies for not merge requesting on GitLab, I'm not as familiar with it as GitHub.

There's just one issue left that I'm having trouble parsing, possibly due to unfamiliarity with pytest. See here:

Does it make sense to you?

@martinpengellyphillips
Copy link
Contributor

Hey Marcus,

Thanks for this.

There seem to be quite a few changes here that don't readily appear relevant to Python 3 support. Could you clarify what needed changing for Python 3?

Regarding the pytest error; it is saying that an expected case is failing and it seems to be because a dict instance is not able to be considered an instance of Resolver. This worked before due to the Resolver.__subclasshook__ testing for a get method, so perhaps there is some change to that behaviour in Python 3 now.

Copy link
Author

@mottosso mottosso left a comment

Choose a reason for hiding this comment

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

Hi Martin, I've highlighted the reasoning to some of the changes, the rest is for PEP08 warnings.

@@ -21,6 +21,7 @@ pip-log.txt

# Unit test / coverage reports
.coverage
.cache
Copy link
Author

Choose a reason for hiding this comment

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

This was created by unit tests in Python 3.

Copy link
Contributor

Choose a reason for hiding this comment

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

👍

from .error import ParseError, FormatError, NotFound


def discover_templates(paths=None, recursive=True):
Copy link
Author

Choose a reason for hiding this comment

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

These were moved into template.py for consistency.

Copy link
Contributor

Choose a reason for hiding this comment

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

I actually had these in __init__.py as the 'high level' API that operates on template instances rather than being templates themselves.

So I don't really see a benefit to changing this at this stage.

get_template
)

__all__ = [
Copy link
Author

Choose a reason for hiding this comment

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

This was added for PEP08, and the "unused variables" warning.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm. My understanding is that __all__ is only required when the package author wants to control behaviour of from package import *, which is considered bad practice anyway.

As I have no need to control this (the default behaviour is fine), I am not clear on the benefit of adding this here. Could you explain further perhaps?

Also, I don't get any "unused variables" warning - what are you using to lint / check the code?

import functools
from collections import defaultdict

import lucidity.error
Copy link
Author

Choose a reason for hiding this comment

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

This was changed to relative, to support vendoring.

Copy link
Contributor

Choose a reason for hiding this comment

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

I deliberately favour absolute imports these days as they keep the namespacing visible and improve clarity.

By 'vendoring' I presume you mean including a package inside another package?

@@ -50,7 +50,7 @@ def test_discover_with_env(path, expected, monkeypatch):
'''Discover templates using environment variable.'''
monkeypatch.setenv('LUCIDITY_TEMPLATE_PATH', path)
templates = lucidity.discover_templates()
assert map(operator.attrgetter('name'), templates) == expected
assert [template.name for template in templates] == expected
Copy link
Author

Choose a reason for hiding this comment

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

Python 3 support.

Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@@ -119,7 +119,7 @@ def test_non_matching_parse(pattern, path, template_resolver):
'''Extract data from non-matching path.'''
template = Template('test', pattern, template_resolver=template_resolver)
with pytest.raises(ParseError):
data = template.parse(path)
Copy link
Author

Choose a reason for hiding this comment

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

Unused variable

Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@@ -252,7 +258,7 @@ def _construct_regular_expression(self, pattern):
else:
_, value, traceback = sys.exc_info()
message = 'Invalid pattern: {0}'.format(value)
raise ValueError, message, traceback #@IgnorePep8
raise ValueError(message, traceback)
Copy link
Author

Choose a reason for hiding this comment

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

Python 3 support.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this would need a Python 2 / 3 switch.

Note: The current syntax is required as a workaround for exception chaining (to maintain original traceback information). I believe Python 3 supports this as raise from.

https://docs.python.org/3/reference/simple_stmts.html#raise

@danbradham
Copy link

Hello!

Just wondering about the status of this pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants