python
                                
                                
                                
                                    python copied to clipboard
                            
                            
                            
                        [New Concept Exercise]: Dataclasses in Python
This issue describes how to implement  the dataclasses concept exercise for the Python track.
The related concept documents issue can be found here.
Additionally, the old concept exercise issue had some discussion attached to it and this concept exercise has been reduced in scope to only dataclasses as a result.  You can find the old issue & discussion here.
β Getting started
If you have not yet created or contributed to a concept exercise, this issue will require some upfront reading to give you the needed background knowledge. Some good example exercises to look at in the repo:
π‘Example Exercisesπ‘ (click to expand)
We also recommend completing one or more of the concept exercises (they're called "learning exercises") on the website.
Please please read the docs before starting. Posting PRs without reading these docs will be a lot more frustrating for you during the review cycle, and exhaust Exercism's maintainers' time. So, before diving into the implementation, please go through the following documents:
General Contributing Docs:
- Contributing to Exercism | Exercism and GitHub | - Contributor Pull Request Guide
 - What are those Weird Task Tags about?
 - Exercism Formatting and Style Guide
 - Exercism Markdown Specification
 - Reputation
 
Documents on Language Tracks and Concept Exercises
- Building Language Tracks: An Overview
 - What are Concept Exercises?
 - Concept Exercise Specifications
 - Concept Exercise Stories
 
π― Goal
The goal of this concept exercise is to teach an understanding/use/creation of dataclasses  in Python.
π‘Learning objectives
- more fully understand the uses (and possible abuses) of 
classesin Python - learn some good use-cases for 
dataclassesover standardclasses - understand when and when not to use a 
dataclasses - create/use 
dataclassesand the@dataclassdecorator - learn some additional methods from the 
dataclassmoduledataclasses.fieldsdataclasses.asdict&dataclasses.astupledataclasses.replace- default factory functions
 - inheritance
 
 
And, if it makes sense and is possible to fit into the exercise:
- Contrast 
dataclasseswith an alternative,collections.namedtuple- pros and cons of each method for creating data-focused 
classes - speed, memory, and typing use considerations
 
 - pros and cons of each method for creating data-focused 
 
π€ Concepts & Related Concepts
classesclass attributesclass membersdataclasses,@dataclassdecoratorscollections.namedtuples()
π« Topics that are Out of scope
Concepts & Subjects that are Out of Scope (click to expand)
class-inheritance,multiple-inheritance,__super()__, classmixins(these all have their own exercises)class-composition-- (beyond the composition needed for the decorators in this exercise)collectionsmodule, outside of what is needed to contrastdataclasseswithcollections.namedtuple(thecollectionsmodule will get its own exercise)comprehensionscoroutinesdecoratorsoutside of@dataclassdescriptors(these will get their own exercise)generatorshigher-order functions- using a 
classas a decorator, beyond the methods made available fordataclasses type aliasestype annotations, beyond what is used indataclasses
β©οΈ Prerequisites
These are the concepts/concept exercises the student should be familiar with before taking on/learning this concept.
basicsbooleansclassesclass-customizationclass-inheritanceclass-compositioncomparisonsdecoratorsdictshigher-order functionsiterationlistsnumberssequencessetsstringstuples
Prereqs (click to expand)
π Resources for Writing and Reference
Resources (click to expand)
- classes (Python tutorial)
 - Python Docs: dataclasses
 - Python Docs: collections.namedtuple()
 - PEP 0557
 - PEP 0526
 - Real Python: Data Classes in Python 3.7+ (Guide)
 - Dan Bader: Writing Clean Python with Namedtuples
 - Trey Hunner: Easier Classes - Python Classes without All the Cruft
 - SO: What are data classes and how are they different?
 - DataClass vs NamedTuple vs Object: A Battle of Performance in Python -- warning, this is a subscription service, so not great for 
links.json - Real Python: Object-Oriented Programming in Python 3
 
Exercise Ideas & Stories
Should you need inspiration for an exercise story, you can find a collection here. You can also port an exercise from another track, but please make sure to only to include tasks that actually make sense in Python and that add value for a student. Remove/replace/add tasks as needed to make the concept clear/workable.
π Exercise Files to Be Created
File Detail for this Exercise (click to collapse)
  | 
βΎοΈ Exercise Metadata - Track
For more information on concept exercises and formatting for the Python track config.json , please see config.json.  The track config.json file can be found in the root of the Python repo.
You can use the below for the exercise UUID. You can also generate a new one via exercism configlet, uuidgenerator.net, or any other favorite method. The UUID must be a valid V4 UUID.
- Exercise UUID :  
807b22ac-cc2c-43ea-b342-c5250227004a - concepts should be filled in from the Concepts section in this issue
 - prerequisites should be filled in from the Prerequisites section in this issue
 
πΆ Implementation Notes
- 
As a reminder, code in the
.meta/examplar.pyfile should only use syntax & concepts introduced in this exercise or one of its prerequisite exercises. We run all ourexamplar.pyfiles through PyLint, but do not strictly require module docstrings. We do require function docstrings similar to PEP257. See this concept exerciseexemplar.pyfor an example. - 
Please do not use comprehensions, generator expressions, or other syntax not previously covered either in the introduction to this exercise, or to one of its prerequisites. Please also follow PEP8 guidelines.
 - 
In General, tests should be written using
unittest.TestCaseand the test file should be named<EXERCISE-NAME>_test.py.- All asserts should contain a "user friendly" failure message (these will display on the webiste to students, so be as clear as you can).
 - We use a 
PyTest custom markto link test cases to exercise task numbers. - We also use 
unittest.subtestto parameterize test input where/when needed. Here is an example testfile that shows all three of these in action. 
 - 
While we do use PyTest as our test runner and for some implementation tests, please check with a maintainer before using a PyTest-specific test method, fixture, or feature.
 - 
Our markdown and JSON files are checked against prettier . We recommend setting prettier up locally and running it prior to submitting your PR to avoid any CI errors.
 
π Next Steps & Getting Help
If you'd like to work on this issue, comment saying "I'd like to work on this"(there is no real need to wait for a response, just go ahead, we'll assign you and put a[claimed]label on the issue).- If you have any questions while implementing, please post the questions as comments in here, or contact one of the maintainers on our Slack channel.
 
I'd like to work on this
@SarvarRaxmonov - That's awesome to hear! π I will assign you. Just a a note -- there is a related issue for writing up the concept of dataclasses. You can find that here. And check the pre-existing discussion under the old issue here.
You don't have to write up the concept (I or someone else can do that) -- but the intro should at least parallel what is gone over in the introduction for this exercise, so we'll want a little coordination for that. π
Assigning this to you...and let me know either here or in our exercism forums if you have any questions or issues.
This issue has been automatically marked as abandoned π because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
That's it. I am turning off the bot, dangit.
@SarvarRaxmonov you're working on this exercise, right?
@okaram -- These issues are not open for community contribution at this time. Thanks.