This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Optional type enforcement
Type: enhancement Stage:
Components: Interpreter Core Versions:
process
Status: closed Resolution: postponed
Dependencies: Superseder:
Assigned To: Nosy List: arigo, georg.brandl, occupant4, rhettinger
Priority: normal Keywords:

Created on 2003-07-28 06:18 by occupant4, last changed 2022-04-10 16:10 by admin. This issue is now closed.

Messages (4)
msg53951 - (view) Author: Matt Perry (occupant4) Date: 2003-07-28 06:18
One of the things that makes Python easy to code in is
the fact that you don't have to explicitly declare the
types of variables, or associate a specific type with a
variable.  However, this can potentially lead to
somewhat confusing and hard-to-maintain code, not to
mention make it easier to introduce bugs.

An idea struck me that it might be possible to combine
the power and safety of a strictly type-checked
language with the speed and ease of coding of Python. 
Normal variables as they stand now would be unaffected
by this feature, and retain their polymorphic type
capabilities.  Allow the programmer to optionally
declare the type of a variable somehow, be it with the
C-like syntax "int x", or Pascal's (I think?) "x: int",
or a new syntactic representation.  Python could then
issue a TypeError if the program attempts to assign a
non-int to x.  Obviously this feature could apply to
any data type, such as string, list, or perhaps
user-defined classes.

Class member variables can be declared as now in the
class definition body, with the same syntax to specify
a type, ie:
   class Test:
       int x = 5
       string name = "test"
       untyped = None

In addition, adding a type specifier to a function's
parameters could serve another means of distinguishing
it from other functions, which could allow function
overloading, ie:
   def double(int x):
       return 2*x
   def double(string x):
       return x + ' ' + x

Advantages:
- clarifies the author's intention in writing code, and
could help prevent confusion as to what type a
variable/parameter is.
- helps prevent bugs due to type mistakes
- doesn't interfere with existing code or coding style,
as dynamically typed variables are still allowed
- could allow function overloading if it was wanted

Disadvantages:
- implementing a type checker could be difficult?
- potentially pollutes namespace with extra keywords
like 'string' or 'list'.  may need to choose different
keywords.
msg53952 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2003-08-05 11:52
Logged In: YES 
user_id=80475

To make the most of polymorphism, interface checking would 
be preferable to enforcing specific types.  For example, it is 
better to check that f is a filelike type rather than requiring 
that is actually be a file -- that way, StringIO objects can be 
substituted without breaking code.
msg53953 - (view) Author: Armin Rigo (arigo) * (Python committer) Date: 2004-05-21 16:25
Logged In: YES 
user_id=4771

This idea has always been considered but there are very deep
issues with it. You should give a look to Pyrex, whose
language a more static and type-checked version of Python.
It produces C extension modules for the regular Python
interpreter, so it can be mixed with regular Python code
easily.  google:python+pyrex
msg53954 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2005-07-17 14:12
Logged In: YES 
user_id=1188172

This isn't going to be in Python 2.x, but presumably in
Python 3k, so I'm closing it for now.

(A change in 2.x would at least require a PEP)
History
Date User Action Args
2022-04-10 16:10:15adminsetgithub: 38944
2003-07-28 06:18:59occupant4create