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 KafkaDsn to networks.py and add default ports for HttpUrl #2447

Merged
merged 12 commits into from Sep 3, 2021

Conversation

MihanixA
Copy link
Contributor

@MihanixA MihanixA commented Mar 2, 2021

❤️ pydantic

Change Summary

  • Added KafkaDsn class e.g. already existing RedisDsn 🔥
  • Added default_parts member and apply_default_parts method to the AnyUrl class

Related issue number

Checklist

  • Unit tests for the changes exist
  • Tests pass on CI and coverage remains at 100%
  • Documentation reflects the changes where applicable
  • changes/<pull request or issue id>-<github username>.md file added describing change
    (see changes/README.md for details)

@codecov
Copy link

codecov bot commented Mar 2, 2021

Codecov Report

Merging #2447 (ba58ec7) into master (7b7e705) will decrease coverage by 0.03%.
The diff coverage is 94.87%.

❗ Current head ba58ec7 differs from pull request most recent head 067e5ef. Consider uploading reports for the commit 067e5ef to get more accurate results

@@             Coverage Diff             @@
##            master    #2447      +/-   ##
===========================================
- Coverage   100.00%   99.96%   -0.04%     
===========================================
  Files           25       25              
  Lines         5109     5134      +25     
  Branches      1050     1051       +1     
===========================================
+ Hits          5109     5132      +23     
- Misses           0        1       +1     
- Partials         0        1       +1     
Impacted Files Coverage Δ
pydantic/__init__.py 100.00% <ø> (ø)
pydantic/networks.py 99.32% <94.87%> (-0.68%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1a9f59d...067e5ef. Read the comment docs.

@MihanixA MihanixA changed the title added KafkaDsn to network added KafkaDsn to networks.py Mar 2, 2021
@davidolrik
Copy link
Contributor

I think this will also allow a fix for my issue #2450, where I want default ports on http urls.

Will a stringification include the defaults? or will the original string be preserved?

@MihanixA
Copy link
Contributor Author

MihanixA commented Mar 2, 2021

I think this will also allow a fix for my issue #2450, where I want default ports on http urls.

Will a stringification include the defaults? or will the original string be preserved?

An example of its behaviour is shown in the test for KafkaDsn model. Default host and port are included to the str version.

https://github.com/samuelcolvin/pydantic/pull/2447/files#diff-f953eed7c7855cc439287af4864f147a1ed9c36995d18f335b57b96e3d7653b3R385

@MihanixA
Copy link
Contributor Author

MihanixA commented Mar 2, 2021

@davidolrik I could commit this solution to this PR and link your issue to this ticket. What do you think?

@MihanixA
Copy link
Contributor Author

MihanixA commented Mar 2, 2021

I've also added default 443 or 80 ports to HttpUrl model which are accessible but not displayed in the stringified version to this PR.
@davidolrik @samuelcolvin

@davidolrik
Copy link
Contributor

Just what I was thinking! 😁

Maybe also add test for http urls with ports included which is also one of the defaults?

m = Model(v='https://www.example.com:443')
assert m.v.port == '443'
assert m.v == 'https://www.example.com:443'

@davidolrik
Copy link
Contributor

I was just playing around with your branch, and I found a bug:

m = Model(v='https://www.example.com:1234')
assert m.v.port == '1234'
assert m.v == 'https://www.example.com:1234'

Here the port is also set to 443:

>       assert m.v.port == '1234'
E       AssertionError: assert '443' == '1234'
E         - 1234
E         + 443

@MihanixA
Copy link
Contributor Author

MihanixA commented Mar 2, 2021

I didn't notice any test that was not expecting to get a ValidationError in test_networks.py with ports other than default (443 and 80).
So for moment I decided that other ports are not expected in this model.

I had already been working on fix by the time I read your message though
But thank you any way! 😄

@MihanixA
Copy link
Contributor Author

MihanixA commented Mar 2, 2021

@davidolrik added tests as you requested

@MihanixA
Copy link
Contributor Author

MihanixA commented Mar 2, 2021

@samuelcolvin ready for review

@MihanixA
Copy link
Contributor Author

MihanixA commented Mar 3, 2021

@PrettyWood I'd appreciate review and/or some feedback 🛩️ 🙏

changes/2447-MihanixA.md Outdated Show resolved Hide resolved
pydantic/networks.py Outdated Show resolved Hide resolved
pydantic/networks.py Outdated Show resolved Hide resolved
pydantic/networks.py Outdated Show resolved Hide resolved
@MihanixA
Copy link
Contributor Author

I couldn't make this work with default port in HttpUrl for now. I really tried though
So I guess I am going to commit fixes for the PR issues and try to solve HttpUrl problem in a separate PR..

@MihanixA
Copy link
Contributor Author

I added some # noqa (pls tell if you want them removed)
And also made HttpUrl inherit AnyHttpUrl instead of AnyUrl

@MihanixA
Copy link
Contributor Author

MihanixA commented Mar 16, 2021

I noticed that CI test "test FastAPI" failed. I run through an error message and I not sure if it's caused by my changes..
UPD noticed that other PR's fail at the same test

@MihanixA MihanixA changed the title added KafkaDsn to networks.py added KafkaDsn to networks.py Mar 19, 2021
@MihanixA
Copy link
Contributor Author

@PrettyWood 🙃 🎉

@@ -114,7 +115,7 @@ class AnyUrl(str):

@no_type_check
def __new__(cls, url: Optional[str], **kwargs) -> object:
return str.__new__(cls, cls.build(**kwargs) if url is None else url)
return str.__new__(cls, cls.build(**kwargs) if url is None else url) # noqa
Copy link
Member

Choose a reason for hiding this comment

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

I would remove all the # noqa. I understand some IDEs like Pycharm will display some kind of warning but as long as flake8 says nothing we're good

@PrettyWood
Copy link
Member

PrettyWood commented Mar 24, 2021

Thanks a lot! 🙏
I guess we could add some type hints for the parts with TypedDict!
I didn't want to commit on your branch so here is a patch for inspiration. You're welcome to use it or not!

For the default port don't we want to add

    @staticmethod
    def get_default_parts(parts: 'Parts') -> 'PartialParts':
        return {
            'port': '80' if parts['scheme'] == 'http' else '443',
        }

I thought this was wanted by @davidolrik in the other issue

And don't worry about the CI 😉 It's due to the new sqlalchemy 1.4 version that is not pinned nor supported by encoded/databases. I opened encode/databases#299

@MihanixA
Copy link
Contributor Author

MihanixA commented Mar 24, 2021

Thank you for feedback!

For the default port don't we want to add

    @staticmethod
    def get_default_parts(parts: 'Parts') -> 'PartialParts':
        return {
            'port': '80' if parts['scheme'] == 'http' else '443',
        }

Maybe use only 'PartialParts' without 'Parts'? By the time get_default_parts is called, parts argument doesn't comply with described TypedDict. Besides this move would allow to avoid code duplication.

UPD
About the issue with default http port: it worked before get_default_parts became a @staticmethod. Now I don't see a clear solution. I'd like to continue to work on that issue in a separate PR when the situation about default parts is finalized)

@MihanixA
Copy link
Contributor Author

@PrettyWood 👻 🥳

@MihanixA
Copy link
Contributor Author

I'd like to continue to work on that issue in a separate PR when the situation about default parts is finalized)

Nevermind :)

@MihanixA MihanixA changed the title added KafkaDsn to networks.py added KafkaDsn to networks.py and add default ports for HttpUrl Mar 26, 2021
@MihanixA MihanixA requested a review from PrettyWood April 3, 2021 11:25
@PrettyWood
Copy link
Member

Can you rebase please ?

@MihanixA
Copy link
Contributor Author

MihanixA commented Apr 5, 2021

@PrettyWood done as you requested.

All tests passed according to CI.
Though when I run tests locally, I get this:

========================================================================================= short test summary info ==========================================================================================
FAILED tests/test_generics.py::test_replace_types - IndexError: tuple index out of range
FAILED tests/test_schema.py::test_unenforced_constraints_schema[kwargs9-type_9] - IndexError: tuple index out of range

Results (57.85s):
    2045 passed
       2 failed
         - tests/test_generics.py:787 test_replace_types
         - tests/test_schema.py:1442 test_unenforced_constraints_schema[kwargs9-type_9]
       6 skipped
make: *** [test] Error 1

Also codecov is less then 100% and I am not sure why :c

@PrettyWood
Copy link
Member

You have all the commits from master that unrelated. Please rebase on the upstream version and push force to have a clean change diff. And we'll see after for the coverage

@MihanixA
Copy link
Contributor Author

MihanixA commented Apr 5, 2021

Looks like I broke everything in this branch(
I am going to close this PR and create a new one with clear change history
OK? @PrettyWood

@PrettyWood
Copy link
Member

Lol no you didn't ;) it's just your rebase that was done on your local version of master and not the upstream one. I can rewrite your history if you need help

@MihanixA
Copy link
Contributor Author

MihanixA commented Apr 5, 2021

Lol no you didn't ;) it's just your rebase that was done on your local version of master and not the upstream one. I can rewrite your history if you need help

I'd appreciate that!

@MihanixA
Copy link
Contributor Author

All tests passed. That is great!
The only thing that remains is to push merge button 👻

@MihanixA
Copy link
Contributor Author

@PrettyWood

@PrettyWood
Copy link
Member

Samuel merges the PRs. And a 1.8.2 may be released before 1.9.

@MihanixA
Copy link
Contributor Author

MihanixA commented May 3, 2021

Samuel merges the PRs. And a 1.8.2 may be released before 1.9.

@PrettyWood ok, thanks! Did you forget to click 'Approve' button or there is something I should fix?

@PrettyWood
Copy link
Member

Thank you!

@PrettyWood PrettyWood merged commit 65fc336 into pydantic:master Sep 3, 2021
@PrettyWood PrettyWood mentioned this pull request Sep 4, 2021
5 tasks
jpribyl pushed a commit to liquet-ai/pydantic that referenced this pull request Oct 7, 2021
…pydantic#2447)

* added KafkaDsn to network

* added short description to chandes folder

* added default non-displayable ports to HttpUrl model

* added info to changes folder

* fix: support non default ports in HttpUrl

* fix pr issues

* remove noqa

* add more typing by @PrettyWood

* add default http and https ports to `HttpUrl` model

* fix mypy

* chore: do not add implementation details

Co-authored-by: PrettyWood <em.jolibois@gmail.com>
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.

None yet

3 participants