Skip to content

Add alias_generator#622

Merged
samuelcolvin merged 10 commits intopydantic:masterfrom
Bobronium:alias-provider
Jul 2, 2019
Merged

Add alias_generator#622
samuelcolvin merged 10 commits intopydantic:masterfrom
Bobronium:alias-provider

Conversation

@Bobronium
Copy link
Copy Markdown
Contributor

@Bobronium Bobronium 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
Copy link
Copy Markdown

codecov Bot 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

@Bobronium Bobronium changed the title Added alias_provider Add alias_provider Jun 27, 2019
Copy link
Copy Markdown
Member

@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

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

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.

Comment thread docs/index.rst Outdated
Comment thread HISTORY.rst Outdated
Comment thread docs/examples/alias_provider_config.py
Comment thread docs/examples/alias_provider_config.py
Comment thread docs/examples/alias_provider_config.py Outdated
Comment thread tests/test_main.py
Comment thread tests/test_main.py Outdated
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
Copy link
Copy Markdown
Member

@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

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

otherwise LGTM.

Comment thread docs/index.rst Outdated
Comment thread docs/index.rst Outdated
Comment thread docs/index.rst Outdated
Bobronium and others added 4 commits June 28, 2019 13:04
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Copy link
Copy Markdown
Member

@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

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

sorry, noticed something else.

Comment thread pydantic/main.py Outdated
Bobronium and others added 3 commits July 2, 2019 13:10
…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 pydantic:master Jul 2, 2019
@samuelcolvin
Copy link
Copy Markdown
Member

great, thank you very much.

@Bobronium Bobronium deleted the alias-provider branch July 2, 2019 11:23
@Bobronium Bobronium restored the alias-provider branch July 2, 2019 11:23
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