Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 311915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:58:06+00:00 2026-05-12T07:58:06+00:00

I didn’t really pay as much attention to Python 3’s development as I would

  • 0

I didn’t really pay as much attention to Python 3’s development as I would have liked, and only just noticed some interesting new syntax changes. Specifically from this SO answer function parameter annotation:

def digits(x:'nonnegative number') -> "yields number's digits":
    # ...

Not knowing anything about this, I thought it could maybe be used for implementing static typing in Python!

After some searching, there seemed to be a lot discussion regarding (entirely optional) static typing in Python, such as that mentioned in PEP 3107, and “Adding Optional Static Typing to Python” (and part 2)

..but, I’m not clear how far this has progressed. Are there any implementations of static typing, using the parameter-annotation? Did any of the parameterised-type ideas make it into Python 3?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T07:58:06+00:00Added an answer on May 12, 2026 at 7:58 am

    Thanks for reading my code!

    Indeed, it’s not hard to create a generic annotation enforcer in Python. Here’s my take:

    '''Very simple enforcer of type annotations.
    
    This toy super-decorator can decorate all functions in a given module that have 
    annotations so that the type of input and output is enforced; an AssertionError is
    raised on mismatch.
    
    This module also has a test function func() which should fail and logging facility 
    log which defaults to print. 
    
    Since this is a test module, I cut corners by only checking *keyword* arguments.
    
    '''
    
    import sys
    
    log = print
    
    
    def func(x:'int' = 0) -> 'str':
        '''An example function that fails type checking.'''
        return x
    
    
    # For simplicity, I only do keyword args.
    def check_type(*args):
        param, value, assert_type = args
        log('Checking {0} = {1} of {2}.'.format(*args))
        if not isinstance(value, assert_type):
            raise AssertionError(
                'Check failed - parameter {0} = {1} not {2}.'
                .format(*args))
        return value
    
    def decorate_func(func):    
        def newf(*args, **kwargs):
            for k, v in kwargs.items():
                check_type(k, v, ann[k])
            return check_type('<return_value>', func(*args, **kwargs), ann['return'])
    
        ann = {k: eval(v) for k, v in func.__annotations__.items()}
        newf.__doc__ = func.__doc__
        newf.__type_checked = True
        return newf
    
    def decorate_module(module = '__main__'):
        '''Enforces type from annotation for all functions in module.'''
        d = sys.modules[module].__dict__
        for k, f in d.items():
            if getattr(f, '__annotations__', {}) and not getattr(f, '__type_checked', False):
                log('Decorated {0!r}.'.format(f.__name__))
                d[k] = decorate_func(f)
    
    
    if __name__ == '__main__':
        decorate_module()
    
        # This will raise AssertionError.
        func(x = 5)
    

    Given this simplicity, it’s strange at the first sight that this thing is not mainstream. However, I believe there are good reasons why it’s not as useful as it might seem. Generally, type checking helps because if you add integer and dictionary, chances are you made some obvious mistake (and if you meant something reasonable, it’s still better to be explicit than implicit).

    But in real life you often mix quantities of the same computer type as seen by compiler but clearly different human type, for example the following snippet contains an obvious mistake:

    height = 1.75 # Bob's height in meters.
    length = len(sys.modules) # Number of modules imported by program.
    area = height * length # What's that supposed to mean???
    

    Any human should immediately see a mistake in the above line provided it knows the ‘human type’ of variables height and length even though it looks to computer as perfectly legal multiplication of int and float.

    There’s more that can be said about possible solutions to this problem, but enforcing ‘computer types’ is apparently a half-solution, so, at least in my opinion, it’s worse than no solution at all. It’s the same reason why Systems Hungarian is a terrible idea while Apps Hungarian is a great one. There’s more at the very informative post of Joel Spolsky.

    Now if somebody was to implement some kind of Pythonic third-party library that would automatically assign to real-world data its human type and then took care to transform that type like width * height -> area and enforce that check with function annotations, I think that would be a type checking people could really use!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 201k
  • Answers 201k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer window.nativeWindow.close(); with jQuery $('a').click(function(){ window.nativeWindow.close(); }); More info May 12, 2026 at 8:13 pm
  • Editorial Team
    Editorial Team added an answer It all depends on what type of development you do.… May 12, 2026 at 8:13 pm
  • Editorial Team
    Editorial Team added an answer Looking at the plan in the 'slow' case it shows… May 12, 2026 at 8:13 pm

Related Questions

Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I didn't get the answer to this anywhere. What is the runtime complexity of
I didn't see any similar questions asked on this topic, and I had to
I didn't see the option to point the workspace (or it's VS equivalent, I'm
I didn't upgrade to Vista until May or so and one of the things

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.