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

Use psycopg2-binary in order to remove Django warnings and fix a typo in docs #25

Merged
merged 12 commits into from
Mar 27, 2019

Conversation

mehdipourfar
Copy link
Contributor

No description provided.

README.md Outdated
pip install -r test_requirements.in
export PYTHONPATH="$(pwd)"
export DJANGO_SETTINGS_MODULE='tests.settings'
django-admin test
Copy link
Owner

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.

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 don't have any experience with tox. Please, correct it yourself.

Copy link
Owner

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

@@ -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)):
Copy link
Owner

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.

Copy link
Contributor Author

@mehdipourfar mehdipourfar Mar 25, 2019

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.

for key in data:
if key.startswith('{}-'.format(name)):
return False
return True
Copy link
Owner

Choose a reason for hiding this comment

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

Break after for block.

@@ -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}))
Copy link
Owner

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.

Copy link
Contributor Author

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

@@ -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' \
Copy link
Owner

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)

Copy link
Contributor Author

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:)

Copy link
Owner

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.

Copy link
Contributor Author

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.

Copy link
Owner

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.

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 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.

Copy link
Owner

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 ?

Copy link
Contributor Author

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)
Copy link
Owner

Choose a reason for hiding this comment

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

👍

@danni
Copy link
Owner

danni commented Mar 26, 2019

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}
Copy link
Owner

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?

Copy link
Contributor Author

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.

Copy link
Owner

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.

@danni danni merged commit 121ecd0 into danni:master Mar 27, 2019
@danni
Copy link
Owner

danni commented Mar 27, 2019

Thanks for this!

@danni danni mentioned this pull request Mar 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants