-
Notifications
You must be signed in to change notification settings - Fork 13
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
Use psycopg2-binary in order to remove Django warnings and fix a typo in docs #25
Conversation
README.md
Outdated
pip install -r test_requirements.in | ||
export PYTHONPATH="$(pwd)" | ||
export DJANGO_SETTINGS_MODULE='tests.settings' | ||
django-admin test |
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.
This is not the accurate. Tests should be run with tox
.
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.
I don't have any experience with tox
. Please, correct it yourself.
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.
You can copy this directly out of the Travis config: https://github.com/danni/django-postgres-composite-types/blob/master/.travis.yml#L22
https://tox.readthedocs.io/en/latest/
pip install tox
tox
Or if you want a specific environment
tox -e py37-dj2.0
While you're at it. Since you're now adding code for Django 2.1, add that to Travis and Tox.ini
postgres_composite_types/forms.py
Outdated
@@ -218,6 +218,12 @@ def value_from_datadict(self, data, files, name): | |||
for subname, widget in self.widgets.items() | |||
} | |||
|
|||
def value_omitted_from_data(self, data, files, name): | |||
for key in data: | |||
if key.startswith('{}-'.format(name)): |
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.
Calculate the comparison name outside the loop and store in a variable.
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.
It has been rewritten in a new commit.
postgres_composite_types/forms.py
Outdated
for key in data: | ||
if key.startswith('{}-'.format(name)): | ||
return False | ||
return True |
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.
Break after for block.
postgres_composite_types/forms.py
Outdated
@@ -146,7 +146,7 @@ def clean(self, value): | |||
except forms.ValidationError as error: | |||
errors.append(prefix_validation_error( | |||
error, code='field_invalid', | |||
prefix='%(label)s: ', params={'label': field.label})) | |||
prefix='%(label)s:', params={'label': field.label})) |
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.
Hmm, I can't explain this space. I think removing it is correct.
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.
Now I found out that this is due to a change in Django 2.1:
Refs #29131
I have created another commit which will fix it.
make error prefix compatible with different django versions
postgres_composite_types/forms.py
Outdated
@@ -144,9 +145,11 @@ def clean(self, value): | |||
try: | |||
cleaned_data[name] = field.clean(value.get(name)) | |||
except forms.ValidationError as error: | |||
prefix = '%(label)s:' if django.__version__ >= '2.1.0' \ |
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.
Avoid unneeded ternary operator.
Also string version compares will fail where semver and string sorting follow different rules. Use distutils.LooseVersion
or compare against the django.VERSION
tuple.
e.g. like this Python check:
PY36 = sys.version_info >= (3, 6)
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.
Okay, I have pushed a new commit.
And thanks a lot. I'm learning from you:)
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.
Don't forget to remove the ternary operator. Much clearer just to assign and use a variable.
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.
Why? I didn't understand.
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.
Ternary operators decrease readability, and serve no purpose outside of pure functional code (comprehensions, lambdas, etc.). In fact, for most of its life Python didn't even have a ternary operator.
condition = snake > 12
result = badger if condition else mushroom
vs
if snake > 12
result = badger
else:
result = condition
The second one is much clearer what result
is. Especially if we have to write multiple different conditions to set result
.
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.
I don't agree about readability when the condition and result values are very simple. Maybe you have guard against it, simply because you don't use it.
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.
See https://pressupinc.com/blog/2015/03/ternary-operators-considered-harmful/
Consider this case for readability:
result = badger if snake else mushroom if owl else simoncowell
If snake
is True
and owl
is False
what's the value of result
?
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.
result is badger because snake is True but I'm against using nested ternary condition. Anyway, I've fix it in a new commit:)
return False | ||
return True | ||
prefix = '{}-'.format(name) | ||
return not any(key.startswith(prefix) for key in data) |
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.
👍
Pylint and isort have also generated a few warnings against your code. It looks like at least one of those warnings if something I just fixed in master. So merge master and fix what's left, and it'll be good to go. |
tox.ini
Outdated
@@ -1,7 +1,7 @@ | |||
[tox] | |||
skipsdist = True | |||
envlist = | |||
py{35,36}-dj{1.11,2.0} | |||
py{35,36}-dj{1.11,2.0, 2.1} |
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.
Does this space here work correctly?
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.
Yes it works. But I have a problem with tests related to django 1.11. I get the following error:
ModuleNotFoundError: No module named 'tests
And also, when I want to run the tests, I will add Postgres password to my database settings. I don't know how your tests pass without setting it.
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.
Let's keep it consistent and remove it I think. Tox is a dark art sometimes.
I run them against a Docker postgres container. Not sure why 1.11 is failing for you there. It is passing in CI.
Thanks for this! |
No description provided.