Giter VIP home page Giter VIP logo

documented's People

Contributors

anatoly-scherbakov avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

documented's Issues

Raising a frozen data class as an exception will err

│ in __setattr__:4                                                                                 │
│ ╭──────────────────────────────────── locals ─────────────────────────────────────╮              │
│ │                 cls = <class 'todoist_costs.errors.MissingDueTime'>             │              │
│ │ FrozenInstanceError = <class 'dataclasses.FrozenInstanceError'>                 │              │
│ │                name = '__traceback__'                                           │              │
│ │                self = MissingDueTime(                                           │              │
│ │                       │   …
│ │                       )                                                         │              │
│ │               value = <traceback object at 0x7f00779cd280>                      │              │
│ ╰─────────────────────────────────────────────────────────────────────────────────╯              │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
FrozenInstanceError: cannot assign to field '__traceback__'

I do not think this can be fixed, it just has to be documented at https://anatoly-scherbakov.github.io/documented/compatibility/dataclasses/ page.

Implement @documented decorator for functions

@documented
def calculate_customers_report(customers: List[Customer], requested_by: User) -> CustomerReport:
    """
    Build customers report.

    Requested by: {requested_by}.
    """

When this function is executed, the following message can be printed:

Build customers report.

Requested by: John Doe.

Before executing the function, its wrapper will substitute the arguments of the function call into the docstring and then will print the docstring using built-in Python logging system.

Purpose of this is to illustrate what is happening in the program in a human readable way in its log.

The project won't build on CI

Run source "$HOME/.poetry/env"
mypy documented tests/*.py
bash: mypy: command not found
make: *** [lint] Error 127
Makefile:5: recipe for target 'lint' failed
Error: Process completed with exit code 2.

Write more about purposes of the library: exceptions + data classes ⇒ control flow

The library documentation refers to a few articles about how exceptions might be used in controlling the control flow of an application. This is an interesting topic to explore further.

I personally very much like to write exceptions as dataclasses and inherit them from DocumentedError. This allows me to encapsulate business logic related information in those exceptions, and then to easily retrieve it in other parts of the application. For instance, I can do something like this:

def ask_hal(question: str, user_name: str):
    try:
        return hal.ask(question)
    except AnswerToAnswer as err:
        raise dataclasses.replace(
            err,
            user_name=user_name,
        )

In this question, we do not provide user_name to the internal layer of the application which is responsible for communication with HAL9000 computer, but we catch the error, enhance it with information available in the API layer, and reraise. Voilà.

Also

ExceptionList, a new feature introduced into Python recently, can be mentioned in relation to this topic.

When?

No idea; perhaps at some point I get inspired by a particular usage example and will get to writing this piece.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.