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

Add alias_generator #622

Merged
merged 10 commits into from Jul 2, 2019

Conversation

Projects
None yet
2 participants
@MrMrRobat
Copy link
Contributor

commented Jun 26, 2019

Change Summary

Add Config.alias_generator that provides ability to convert name to alias for each field of model. See docs example:

from pydantic import BaseModel

def to_camel(string: str) -> str:
     return ''.join(word.capitalize() for word in string.split('_'))
 
class Voice(BaseModel):
    name: str
    gender: str
    language_code: str
    
    class Config:
         alias_generator = to_camel

 voice = Voice(Name='Filiz', Gender='Female', LanguageCode='tr-TR')
 print(voice.language_code)
 print(voice.dict(by_alias=True))

  """
 tr-TR
 {'Name': 'Filiz', 'Gender': 'Female', 'LanguageCode': 'tr-TR'}
 """

Related issue number

None

Checklist

  • Unit tests for the changes exist
  • Tests pass on CI and coverage remains at 100%
  • Documentation reflects the changes where applicable
  • HISTORY.rst has been updated
@codecov

This comment has been minimized.

Copy link

commented Jun 26, 2019

Codecov Report

Merging #622 into master will not change coverage.
The diff coverage is 100%.

@@          Coverage Diff          @@
##           master   #622   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          15     15           
  Lines        2628   2634    +6     
  Branches      516    518    +2     
=====================================
+ Hits         2628   2634    +6

@MrMrRobat MrMrRobat changed the title Added alias_provider Add alias_provider Jun 27, 2019

@samuelcolvin
Copy link
Owner

left a comment

Otherwise mostly looks good to me.

How do we cope with #624 which I hope to implement soon? Three different functions or the method could alternatively return a 3-tuple? Either of these solutions could be implemented later and would be backwards compatible.

Show resolved Hide resolved docs/index.rst Outdated
Show resolved Hide resolved HISTORY.rst Outdated
Show resolved Hide resolved docs/examples/alias_provider_config.py Outdated
Show resolved Hide resolved docs/examples/alias_provider_config.py Outdated
Show resolved Hide resolved docs/examples/alias_provider_config.py Outdated
Show resolved Hide resolved tests/test_main.py
Show resolved Hide resolved tests/test_main.py Outdated
Rename alias_provider —> alias_generator
Move docs to config section, reformat example
Separate test_alias_generator

@samuelcolvin samuelcolvin changed the title Add alias_provider Add alias_generator Jun 28, 2019

@samuelcolvin
Copy link
Owner

left a comment

otherwise LGTM.

Show resolved Hide resolved docs/index.rst Outdated
Show resolved Hide resolved docs/index.rst Outdated
Show resolved Hide resolved docs/index.rst Outdated

MrMrRobat and others added some commits Jun 28, 2019

Update docs/index.rst
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Update docs/index.rst
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Update docs/index.rst
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
@samuelcolvin
Copy link
Owner

left a comment

sorry, noticed something else.

Show resolved Hide resolved pydantic/main.py Outdated

MrMrRobat and others added some commits Jul 2, 2019

Fix conflict between dictionary field config in Config.fields and Con…
…fig.alias_generator


Add one more condition for applying alias_generator

Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>

@samuelcolvin samuelcolvin merged commit 010ba38 into samuelcolvin:master Jul 2, 2019

5 of 8 checks passed

Header rules No header rules processed
Details
Pages changed All files already uploaded
Details
Redirect rules No redirect rules processed
Details
LGTM analysis: Python No new or fixed alerts
Details
Mixed content No mixed content detected
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
deploy/netlify Deploy preview ready!
Details
samuelcolvin.pydantic Build #20190702.3 succeeded
Details
@samuelcolvin

This comment has been minimized.

Copy link
Owner

commented Jul 2, 2019

great, thank you very much.

@MrMrRobat MrMrRobat deleted the MrMrRobat:alias-provider branch Jul 2, 2019

@MrMrRobat MrMrRobat restored the MrMrRobat:alias-provider branch Jul 2, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.